Microsoft KB Archive/811399

= How to display the ContextMenu that is specific to a highlighted node in a TreeView control in Visual Basic 2005 or in Visual Basic .NET =

Article ID: 811399

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SUMMARY
This step-by-step article describes how to display the ContextMenu that is specific to a highlighted node in a TreeView control. You may display different menus for different nodes in a TreeView. The node that the user right-clicks may not be the selected node. This article describes how to go to the highlighted node, and then how to show the menu for the highlighted node.

Create a TreeView Windows Application
 Create a new Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET Windows Application project.

By default, Form1 is displayed. From the toolbox, drag a TreeView control onto Form1. In the designer pane, double-click Form1 to open the Code window.  To add nodes to the TreeView, add the following code to the Form1_Load event: ' Create node for TreeView Dim node As TreeNode node = New TreeNode(&quot;File&quot;)

' Add Tag to the node for identifying the node type ' This Tag would be used to identify the context menu associated with it     node.Tag = &quot;TextFile&quot;

' Add the node to the TreeView TreeView1.Nodes.Add(node)

node = New TreeNode(&quot;File1&quot;) node.Tag = &quot;File&quot; TreeView1.Nodes(0).Nodes.Add(node)

node = New TreeNode(&quot;File2&quot;) node.Tag = &quot;File&quot; TreeView1.Nodes(0).Nodes.Add(node) 

Add a ContextMenu to the Application
 In the Solution Explorer, right-click Form1.vb, and then click View Designer. In the toolbox, double-click ContextMenu to add a context menu. Right-click ContextMenu1, and then click Properties.</li> In the Properties window, change the Name to mnuTextFile .</li> In the designer pane, click ContextMenu on Form1 to add a submenu to mnuTextFile.</li> Click Type Here, and then type New File. Press the ENTER key.</li> Right-click New File, and then click Properties. In the Properties window, change the Name to mnuNewFile .</li> Repeat step 2. In the Properties window, change the Name to mnuFile .</li> Repeat step 4, step 5, and step 6 to add a submenu to mnuFile. Change the Name to mnuOpen, and then change the Text to Open.</li> Add another submenu to mnuFile. Change the Name to mnuClose, and change the Text to Close.</li> In the Solution Explorer, right-click Form1.vb, and then click View Code.</li>  Change the following code to class Form1: ' New File menu handler Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNewFile.Click MessageBox.Show(&quot;New file menu clicked&quot;) End Sub ' Open menu handler Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click MessageBox.Show(&quot;Open file menu clicked&quot;) End Sub ' Close menu handler Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClose.Click MessageBox.Show(&quot;Close file menu clicked&quot;) End Sub </li></ol>

Display the ContextMenu That Is Specific to the Highlighted Node
 In the Solution Explorer, right-click Form1.vb, and then click View Code.</li>  Change the following code to the class Form1: Private m_OldSelectNode As TreeNode Private Sub TreeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseUp ' Show menu only if Right Mouse button is clicked If e.Button = MouseButtons.Right Then

' Point where mouse is clicked Dim p As Point = New Point(e.X, e.Y)

' Go to the node that the user clicked Dim node As TreeNode = TreeView1.GetNodeAt(p) If Not node Is Nothing Then

' Highlight the node that the user clicked. ' The node is highlighted until the Menu is displayed on the screen m_OldSelectNode = TreeView1.SelectedNode TreeView1.SelectedNode = node

' Find the appropriate ContextMenu based on the highlighted node Select Case node.Tag Case &quot;TextFile&quot; mnuTextFile.Show(TreeView1, p)              Case &quot;File&quot; mnuFile.Show(TreeView1, p)           End Select

' Highlight the selected node TreeView1.SelectedNode = m_OldSelectNode m_OldSelectNode = Nothing

End If     End If   End Sub </li></ol>

Test the Application

 * 1) On the Debug menu, click Start.

Form1 is displayed.
 * 1) By default, File node is selected.

Expand File.
 * 1) Right-click File.

The New File menu is displayed.
 * 1) Click New File, and then click OK.
 * 2) Right-click File1.

The Open and the Close menus are displayed.

The File1 node is highlighted, although the File node is selected.
 * 1) Click Open, and then click OK.

The Files node is highlighted.

<div class="references_section">