2013-02-18 60 views
0

我有一个包含datagridview的窗体。每当用户点击一行时,一个小面板就会出现在行的下方并显示一些按钮。如预期工作一切都与下面的代码:如何防止出现在屏幕外的面板?

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
    { 

     rowSelected = e.RowIndex; 
     columnSelected = e.ColumnIndex; 

     if (e.RowIndex == -1) return; 

     var cellRectangle = dataGridView1.PointToScreen(
     dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location); 
     panel_EditButtons.Location = new Point(cellRectangle.X + 50, cellRectangle.Y); 

     panel_EditButtons.Show(); 

    } 

能有一个人请告诉我如何防止面板出现了屏幕的时候对行的用户点击,也没有足够的空间来显示面板?观察这两种图像I附:


普通视图(当有足够的空间来显示面板时,面板显示正确)

enter image description here

当我点击该行的右侧(并且没有足够的空间来显示面板,该面板显示出画面)
enter image description here

能有一个人请告诉我,我怎么能显示完整的面板,甚至当我点击该行的右侧,面板没有足够的空间位置仅限于屏幕宽度?

回答

0

我知道的唯一方法是重置坐标。

if (panel_EditButtons.Location.X + panel_EditButtons.Size.X > width) 
    panel_EditButtons.Location = new Point(width - panel_EditButtons.Size.X, panel_EditButtons.Location.Y); 
// same with Y 

请记住,我的代码只是伪代码,它可能不会通过简单的复制粘贴工作。