2013-02-18 62 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 

請記住,我的代碼只是僞代碼,它可能不會通過簡單的複製粘貼工作。