如何防止面板出现在屏幕外?

时间:2013-02-18 15:47:19

标签: c# .net datagridview

我有一个包含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();

    }

有人可以告诉我当用户点击一行并且没有足够的空间来显示面板时,如何防止面板出现在屏幕外面?看看我附上的两张图片:

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

enter image description here

当我点击该行的右侧(并且没有足够的空间显示该面板时,该面板显示在屏幕外)
enter image description here

有人可以告诉我即使我点击行的右侧并且没有足够的空间以便面板位置仅限于屏幕宽度,我怎样才能显示完整的面板?

1 个答案:

答案 0 :(得分: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

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

相关问题