)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize the contextmenu to display the name of the selected node when right clicked on it in WinForms TreeViewAdv?

Platform: WinForms |
Control: TreeViewAdv |
Published Date: April 30, 2011 |
Last Revised Date: February 5, 2020

Customize the context menu

The user could customize the context menu to display the name of the selected node when right clicked on the SelectedNode by handling the MouseDown Event handler in the following way.The user have to get the right click point of the selectedNode.Then if the selectedNode is not equal to null, the a new menu item is created and added,such that the item displays the selectedNode's text name. For displaying only, the selectedNode text at a time when right clicked, the menu items must be cleared.

C#

private void treeViewAdv1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
   if(e.Button==MouseButtons.Right)
   {
       //Clear the contextMenu Menuitems
       this.contextMenu1.MenuItems.Clear();
       this.treeViewAdv1.SelectedNode=this.treeViewAdv1.GetNodeAtPoint(new Point(e.X,e.Y));
       //If the selected node is not equal to null, check for the condition
       if(this.treeViewAdv1.SelectedNode!=null)
       {
           //Create MenuItem for the SelectedNode
           MenuItem menuitem=new MenuItem(this.treeViewAdv1.SelectedNode.Text);
           this.contextMenu1.MenuItems.Add(menuitem);
       }
   }
}

VB

Private Sub treeViewAdv1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
   If e.Button=MouseButtons.Right Then
      'Clear the contextMenu Menuitems
      Me.contextMenu1.MenuItems.Clear()
      Me.treeViewAdv1.SelectedNode=Me.treeViewAdv1.GetNodeAtPoint(New Point(e.X,e.Y))
      'If the selected node is not equal to null, check for the condition
      If Not Me.treeViewAdv1.SelectedNode Is Nothing Then
         'Create MenuItem for the SelectedNode
         Dim menuitem As MenuItem = New MenuItem(Me.treeViewAdv1.SelectedNode.Text)
         Me.contextMenu1.MenuItems.Add(menuitem)
      End If
   End If
End Sub

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon