Microsoft KB Archive/810001

From BetaArchive Wiki

Article ID: 810001

Article Last Modified on 11/13/2007



APPLIES TO

  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition



SUMMARY

This step-by-step article describes how to display the context menu that is specific to a selected node in a TreeView control. You can display different menus for different nodes in a tree view. The node that the user right-clicks may not be the selected node. This article describes how to get the selected node and then show the menu that is specific to the selected node.

back to the top

Create a Windows Application Project That Includes a TreeView Control

  1. Create a new Visual C# .NET or Visual C# 2005 Windows Application project. By default, Form1 is created.

    Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are called Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:
  2. From the toolbox, drag a TreeView control to the form.
  3. In the Designer pane, double-click Form1 to open the code window.
  4. To add nodes to the TreeView control, add the following code to the Form1_Load event:

     // Create a node for the TreeView control.
        TreeNode node;
        node = new TreeNode("File");
    
        // Add a tag to the node for identifying the node type.
        // This tag is used to identify the context menu that is associated with it.
        node.Tag = "TextFile";
    
        // Add the node to the TreeView control.
        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);

back to the top

Add a Context Menu to the Application


  1. In Solution Explorer, right-click Form1.cs, and then click View Designer.
  2. To add a context menu, in the toolbox, double-click ContextMenu. Right-click ContextMenu1, and then click Properties. In the Properties dialog box, change the name to mnuTextFile.
  3. To add a sub menu to mnuTextFile, in the Designer pane, click Context Menu that is displayed on Form1.
  4. Click Type Here, type New File, and then press ENTER.
  5. Right-click New File, and then click Properties. In the Properties dialog box, change the name to mnuNewFile.
  6. Repeat step 2 to add another context menu from the toolbox, and then name it mnuFile.
  7. Repeat steps 3-5 to add a sub menu to the mnuFile. Change the name to mnuOpen, and then change the text to Open.
  8. Add another sub menu to the mnuFile menu. Change the name to mnuClose, and then change the text to Close.
  9. In Solution Explorer, right-click Form1.cs, and then click View Code.
  10. To get event handlers for the menu, add the following code to the InitializeComponent method:

    this.mnuNewFile.Click += new System.EventHandler(this.mnuNewFile_Click);
    this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
    this.mnuClose.Click += new System.EventHandler(this.mnuClose_Click);
    this.treeView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseUp);
  11. Add the following code to the Form1 class:

         private void mnuNewFile_Click(object sender, System.EventArgs e)
            {
                MessageBox.Show("New file menu clicked");
            }
    
            private void mnuOpen_Click(object sender, System.EventArgs e)
            {
                MessageBox.Show("Open file menu clicked");
            }
    
            private void mnuClose_Click(object sender, System.EventArgs e)
            {
                MessageBox.Show("Close file menu clicked");
            }

back to the top

Display a Context Menu That Is Specific to the Selected Node

  1. In Solution Explorer, right-click Form1.cs, and then click View Code.
  2. Add the following code to the Form1 class:

    private TreeNode m_OldSelectNode;
    private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        // Show menu only if the right mouse button is clicked.
        if (e.Button == MouseButtons.Right )
        {
    
            // Point where the mouse is clicked.
            Point p = new Point(e.X, e.Y);
    
            // Get the node that the user has clicked.
            TreeNode node= treeView1.GetNodeAt(p);
            if (node != null)
            {
    
                // Select the node the user has clicked.
                // The node appears selected until the menu is displayed on the screen.
                m_OldSelectNode = treeView1.SelectedNode;
                treeView1.SelectedNode = node;
    
                // Find the appropriate ContextMenu depending on the selected node.
                switch(Convert.ToString(node.Tag))
                {
                    case "TextFile":
                        mnuTextFile.Show(treeView1, p);
                        break;
                    case "File":
                        mnuFile.Show(treeView1, p);
                        break;
                }
    
                // Highlight the selected node.
                treeView1.SelectedNode = m_OldSelectNode;
                m_OldSelectNode = null;
            }
        }
    }

back to the top

Test the Application

  1. On the Debug menu, click Start. Form1 is displayed. By default, the File node is selected.
  2. Expand the File node.
  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.
  6. The File1 node appears selected, although File is the selected node.
  7. Click Open, and then click OK. The File node appears selected.

back to the top

REFERENCES

For additional information, see the following Microsoft Developer Network (MSDN) Web sites:
ContextMenu class

TreeView

back to the top

C# migration of 811399.

Keywords: kbhowto kbhowtomaster kbwindowsforms kbtreeview kbcontmenu kbctrl kbcontrol KB810001