【问题标题】:VB.NET Windows Forms context menu positionVB.NET Windows 窗体上下文菜单位置
【发布时间】:2013-02-26 15:25:42
【问题描述】:

我正在增强以前编写的 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 中,如果是右键单击并且用户单击了数据行,那么我希望上下文菜单显示鼠标所在的位置,而不是顶部!

【问题讨论】:

    标签: vb.net properties datagridview position contextmenu


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多