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 (811399)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SUMMARYThis 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("File")
' 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 = "TextFile"
' Add the node to the TreeView
TreeView1.Nodes.Add(node)
node = New TreeNode("File1")
node.Tag = "File"
TreeView1.Nodes(0).Nodes.Add(node)
node = New TreeNode("File2")
node.Tag = "File"
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.
- In the Properties window, change the Name
to mnuTextFile.
- In the designer pane, click ContextMenu on
Form1 to add a submenu to mnuTextFile.
- Click Type Here, and then type
New File. Press the ENTER key.
- Right-click New File, and then click
Properties. In the Properties window, change the
Name to mnuNewFile.
- Repeat step 2. In the Properties window, change the
Name to mnuFile.
- 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.
- Add another submenu to mnuFile. Change the
Name to mnuClose, and change the
Text to Close.
- In the Solution Explorer, right-click
Form1.vb, and then click View
Code.
- 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("New file menu clicked")
End Sub
' Open menu handler
Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
MessageBox.Show("Open file menu clicked")
End Sub
' Close menu handler
Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClose.Click
MessageBox.Show("Close file menu clicked")
End Sub
Display the ContextMenu That Is Specific to the Highlighted Node- In the Solution Explorer, right-click
Form1.vb, and then click View
Code.
- 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 "TextFile"
mnuTextFile.Show(TreeView1, p)
Case "File"
mnuFile.Show(TreeView1, p)
End Select
' Highlight the selected node
TreeView1.SelectedNode = m_OldSelectNode
m_OldSelectNode = Nothing
End If
End If
End Sub
Test the
Application- On the Debug menu, click
Start.
Form1 is displayed. - By default, File node is
selected.
Expand File. - Right-click File.
The
New File menu is displayed. - Click New File, and then click
OK.
- Right-click File1.
The
Open and the Close menus are
displayed.
The File1 node is highlighted, although
the File node is selected. - Click Open, and then click
OK.
The Files node is
highlighted.
REFERENCESFor more information, visit following Microsoft Web
site:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbWindowsForms kbTreeView kbCtrl kbContMenu kbControl kbHOWTOmaster kbhowto KB811399 kbAudDeveloper |
---|
|