嗯,你好!只是有一个关于我拥有的代码的快速问题。当我右键单击 DataGridView 中的单元格时,我尝试打开上下文菜单。这是我所拥有的:
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
If e.ColumnIndex = -1 = False And e.RowIndex = -1 = False Then
Me.DataGridView1.ClearSelection()
Me.DataGridView1.CurrentCell = Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex)
DataGridView1.ContextMenuStrip = mnuCell
End If
End If
End Sub
不幸的是,当我第一次右键单击该程序时,它不会立即打开上下文菜单。它仅选择单元格。但如果我再次右键单击它,它将打开上下文菜单。
我的第二个问题是,如果我在上下文菜单仍然打开的情况下右键单击另一个单元格,它不会选择我右键单击的另一个单元格。我做错了什么?
最佳答案
上下文菜单将在 CellMouseClick 事件触发之前弹出,因此请将代码移至 CellMouseDown。
Private Sub DataGridView1_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
If e.ColumnIndex <> -1 And e.RowIndex <> -1 Then
Me.DataGridView1.ClearSelection()
Dim cell = Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex)
Me.DataGridView1.CurrentCell = cell
cell.Selected = True 'Needed if you right click twice on the same cell
DataGridView1.ContextMenuStrip = mnuCell
End If
End If
End Sub
关于vb.net - 在 DataGridView 上打开上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904502/