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

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

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

    By default, Form1 is displayed.
  2. From the toolbox, drag a TreeView control onto Form1.
  3. In the designer pane, double-click Form1to open the Code window.
  4. 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

  1. In the Solution Explorer, right-click
    Form1.vb, and then click View Designer.
  2. In the toolbox, double-click ContextMenuto add a context menu. Right-click ContextMenu1, and then click Properties.
  3. In the Properties window, change the Nameto mnuTextFile.
  4. In the designer pane, click ContextMenu on Form1 to add a submenu to mnuTextFile.
  5. Click Type Here, and then type
    New File. Press the ENTER key.
  6. Right-click New File, and then click
    Properties. In the Properties window, change the
    Name to mnuNewFile.
  7. Repeat step 2. In the Properties window, change the
    Name to mnuFile.
  8. 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.
  9. Add another submenu to mnuFile. Change the
    Name to mnuClose, and change the
    Text to Close.
  10. In the Solution Explorer, right-click
    Form1.vb, and then click View Code.
  11. 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

  1. In the Solution Explorer, right-click
    Form1.vb, and then click View Code.
  2. 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

  1. On the Debug menu, click
    Start.

    Form1 is displayed.
  2. By default, File node is selected.

    Expand File.
  3. Right-click File.

    The
    New File menu is displayed.
  4. Click New File, and then click
    OK.
  5. Right-click File1.

    The
    Open and the Close menus are displayed.

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

    The Files node is highlighted.

References

For more information, visit following Microsoft Web site:
Propriedades

ID do Artigo: 811399 - Última Revisão: 13 de nov de 2007 - Revisão: 1

Comentários