Category / Section
How to show the context menu on key selection in the WinForms TreeViewAdv?
1 min read
Show context menu on key selection
In the TreeViewAdv, Context Menu is shown on key selection by handling the KeyUp event.
C#
//The event is raised when pressed key is released. this.treeViewAdv1.KeyUp += new KeyEventHandler(treeViewAdv1_KeyUp); void treeViewAdv1_KeyUp(object sender, KeyEventArgs e) { if(e.KeyValue == 93) { //Specifies the location where Context menu needs to be shown. int x = treeViewAdv1.SelectedNode.TextBounds.X; int y = treeViewAdv1.SelectedNode.Bounds.Y + treeViewAdv1.SelectedNode.Bounds.Height; Point point = new Point(x, y); Point ContextPoint = treeViewAdv1.PointToScreen(point); //Shows the ContextMenuStripEx. this.contextMenuStripEx1.Show((ContextPoint)); } }
VB
'The event is raised when pressed key is released. AddHandler treeViewAdv1.KeyUp, AddressOf treeViewAdv1_KeyUp Private Sub treeViewAdv1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) If e.KeyValue = 93 Then 'Specifies location where Context menu needs to be shown. Dim x As Integer = treeViewAdv1.SelectedNode.TextBounds.X Dim y As Integer = treeViewAdv1.SelectedNode.Bounds.Y + treeViewAdv1.SelectedNode.Bounds.Height Dim point As New Point(x, y) Dim ContextPoint As Point = treeViewAdv1.PointToScreen(point) 'Shows the ContextMenuStripEx. Me.contextMenuStripEx1.Show((ContextPoint)) End If End Sub
Figure 1: ContextMenuStripEx seen under the selected TreeNodeAdv
Samples: