根据不同对象的状态更改 contextMenuStrip
Change contextMenuStrip based on state of a different object
我正在寻找一种在 VB.net 中实现 TreeView 的好方法,其 contextMenuStrip 是基于表单中不同对象的状态而变化的。
具体来说,在下面的 'TreeView' 中,当对象 state=1 时,在项目上显示 contextMenuStrip1,当 state=2 时,在项目上显示 contextMenuStrip2。
到目前为止,我一直在像下面的代码一样实现上下文菜单,并在创建节点时添加 contextMenuStrip。
Dim Context1 As ContextMenuStrip = New ContextMenuStrip
AddHandler Context1.Items.Add("Delete Item").Click, AddressOf DeleteSub
------------------
| + TreeNode1
| + TreeNode2
| + TreeNode3
| |- Item1
| |- Item2
| |- Item3
------------------
我不知道这个决定要使用的菜单的对象是什么,所以我使用了 CheckBox
。如果有问题的对象是这样的,您可以在状态更改时重新分配 ContextMenuStrip
- 在这种情况下,使用 CheckChange
事件:
Private Sub chkShow2_CheckedChanged(sender...
If chkShow2.Checked Then
tv1.ContextMenuStrip = cms2
Else
tv1.ContextMenuStrip = cms1
End If
End Sub
如果在需要菜单时才知道状态,re/assign TreeView
的 MouseDown
事件中的菜单:
If e.Button = Windows.Forms.MouseButtons.Right Then
If chkShow2.Checked Then
tv1.ContextMenuStrip = cms2
Else
tv1.ContextMenuStrip = cms1
End If
End If
您也可以手动显示菜单,而不是将其分配给控件:
Private Sub tv1_MouseDown(sender ...
If e.Button = Windows.Forms.MouseButtons.Right Then
If chkShow2.Checked Then
cms2.Show(tv1, e.Location)
Else
cms1.Show(tv1, e.Location)
End If
End If
End Sub
Would it also be possible to append a number to the contextMenuStrip item?
是的。您可以 add/change 或在显示之前删除菜单项。例如,如果您有节点 {A, B, C}
,并且想要实现一个 Move To...
项目,当它打开并且项目位于节点 B 中时,请禁用该目的地。
我正在寻找一种在 VB.net 中实现 TreeView 的好方法,其 contextMenuStrip 是基于表单中不同对象的状态而变化的。
具体来说,在下面的 'TreeView' 中,当对象 state=1 时,在项目上显示 contextMenuStrip1,当 state=2 时,在项目上显示 contextMenuStrip2。
到目前为止,我一直在像下面的代码一样实现上下文菜单,并在创建节点时添加 contextMenuStrip。
Dim Context1 As ContextMenuStrip = New ContextMenuStrip
AddHandler Context1.Items.Add("Delete Item").Click, AddressOf DeleteSub
------------------
| + TreeNode1
| + TreeNode2
| + TreeNode3
| |- Item1
| |- Item2
| |- Item3
------------------
我不知道这个决定要使用的菜单的对象是什么,所以我使用了 CheckBox
。如果有问题的对象是这样的,您可以在状态更改时重新分配 ContextMenuStrip
- 在这种情况下,使用 CheckChange
事件:
Private Sub chkShow2_CheckedChanged(sender...
If chkShow2.Checked Then
tv1.ContextMenuStrip = cms2
Else
tv1.ContextMenuStrip = cms1
End If
End Sub
如果在需要菜单时才知道状态,re/assign TreeView
的 MouseDown
事件中的菜单:
If e.Button = Windows.Forms.MouseButtons.Right Then
If chkShow2.Checked Then
tv1.ContextMenuStrip = cms2
Else
tv1.ContextMenuStrip = cms1
End If
End If
您也可以手动显示菜单,而不是将其分配给控件:
Private Sub tv1_MouseDown(sender ...
If e.Button = Windows.Forms.MouseButtons.Right Then
If chkShow2.Checked Then
cms2.Show(tv1, e.Location)
Else
cms1.Show(tv1, e.Location)
End If
End If
End Sub
Would it also be possible to append a number to the contextMenuStrip item?
是的。您可以 add/change 或在显示之前删除菜单项。例如,如果您有节点 {A, B, C}
,并且想要实现一个 Move To...
项目,当它打开并且项目位于节点 B 中时,请禁用该目的地。