2013-02-26 50 views
2

我提高以前編寫的VB.NET Windows窗體應用程序。VB.NET Windows窗體上下文菜單位置

在的一種形式,有在窗體加載從存儲過程填充一個DataGridView。

如果用戶擁有的權限,當用戶在DataGridView中的記錄鼠標右擊,我想顯示上下文菜單,允許用戶刪除的記錄。

這裏是我的代碼:

Private Sub m_SetEnabledContextMenu() 
    ' for Delete record 
    If Not objUser.HasAuthority("Delete Record") Then 
    Me.DataGridView1.ContextMenu = Nothing 
    Me.mnuContextEquationSubmission.Enabled = False 
    Else 
    ' Me.DataGridView1.ContextMenu = Me.mnuContextEquationSubmission 
    Me.mnuContextEquationSubmission.Enabled = True 
    End If 
    'Rest is not problem 
End Sub 

Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, _ 
     ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _ 
      Handles DataGridView1.CellMouseDown 
    If e.Button = System.Windows.Forms.MouseButtons.Right Then 
    For Each row As DataGridViewRow In DataGridView1.SelectedRows 
     row.Selected = False 
    Next 
    DataGridView1.Rows(e.RowIndex).Selected = True 

    'MessageBox.Show("DataGridView1_CellMouseDown on row " & e.RowIndex.ToString) 
    m_intSelRow = e.RowIndex 
    m_intSelCol = e.ColumnIndex 

    m_strRecordID = DataGridView1.Rows(e.RowIndex).Cells(0).Value 

    'mnuContextEquationSubmission.Show(DataGridView1, e.Location) 
    mnuContextEquationSubmission.Show(CType(sender, Control), e.Location) 
    End If 
End Sub 

,你可以看到,在過程「m_SetEnabledContextMenu」我確定不是用戶將不得不刪除記錄的權限,因此,上下文菜單將被激活(或者至少,這就是我想要的) - 而用戶沒有權限,菜單應該是無形的和殘疾。

在事件處理程序DataGridView1_CellMouseDown中,如果它是右鍵單擊,並且用戶點擊了數據行,那麼我希望上下文菜單顯示鼠標所在的位置,而不是頂部!

回答

4

使用mnuContextEquationSubmission.Show(Me, Me.PointToClient(MousePosition))