我使用的是 UltraGrid
,它有一个包含 2 个项目的 ContextMenuStrip
。右键单击 UltraGridCell
时会显示这些内容。
但是,为了显示它们,用户必须首先单击单元格以使其退出编辑模式,然后右键单击它以显示ContextMenuStripItems
。
这已经让用户感到困惑和恼火,所以我想知道是否有任何方法可以更改它以在仍处于编辑模式时右键单击时显示它们?
我尝试过在按下某个键后将其退出编辑模式,但它不起作用。
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
我还在 Infragistics 论坛上建议的 MouseClick
中尝试了一些方法,但同样不起作用。
是否有任何方法可以让用户右键单击处于编辑模式的单元格可以调出 ContextMenuStripItems
而不是此菜单?
上图显示了在编辑模式下右键单击单元格时当前显示的内容(该单元格是右下角的白色单元格)。我不希望出现此内容,而是希望 CMS 出现。
编辑
我已经尝试了当前答案中的建议,但这些建议都不适合我。可能是因为网格的版本稍旧?
我最近的工作是使用以下代码完成的:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
但是直到单元格不再处于编辑模式时才会触发此操作。
以下答案都不能解决问题。仍然需要答案
最佳答案
当网格的任何单元格进入编辑模式时,将在该单元格上绘制一个文本框。这里最好的部分是这个文本框可重复用于网格中的所有单元格。当您在编辑模式下右键单击单元格时,将显示来自 MS 的默认上下文菜单。您需要做的就是获取此文本框并将其分配给您的上下文菜单条。您可以通过处理网格的 ControlAdded 事件来完成此操作,如下所示:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
关于vb.net - 显示 ContextMenuStripItem 而不单击当前单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43915396/