Microsoft KB Archive/810001

= How to display a context menu that is specific to a selected TreeView node by using Visual C# .NET or Visual C# 2005 =

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

-





For a Microsoft Visual Basic .NET version of this article, see 811399.

IN THIS TASK

 * SUMMARY
 * Create a Windows Application Project That Includes a TreeView Control
 * Add a Context Menu to the Application
 * Display a Context Menu That Is Specific to the Selected Node
 * Test the Application
 * REFERENCES



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
 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:

http://msdn2.microsoft.com/en-us/library/ms173077.aspx

 From the toolbox, drag a TreeView control to the form. In the Designer pane, double-click Form1 to open the code window.  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(&quot;File&quot;);

// 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 = &quot;TextFile&quot;;

// Add the node to the TreeView control. 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); 

back to the top

Add a Context Menu to the Application
 In Solution Explorer, right-click Form1.cs, and then click View Designer. 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 .</li> To add a sub menu to mnuTextFile, in the Designer pane, click Context Menu that is displayed on Form1.</li> Click Type Here, type New File, and then press ENTER .</li> Right-click New File, and then click Properties. In the Properties dialog box, change the name to mnuNewFile .</li> Repeat step 2 to add another context menu from the toolbox, and then name it mnuFile.</li> Repeat steps 3-5 to add a sub menu to the mnuFile. Change the name to mnuOpen, and then change the text to Open .</li> Add another sub menu to the mnuFile menu. Change the name to mnuClose, and then change the text to Close .</li> In Solution Explorer, right-click Form1.cs, and then click View Code.</li>  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); </li>  Add the following code to the Form1 class: private void mnuNewFile_Click(object sender, System.EventArgs e)       { MessageBox.Show(&quot;New file menu clicked&quot;); }

private void mnuOpen_Click(object sender, System.EventArgs e)       { MessageBox.Show(&quot;Open file menu clicked&quot;); }

private void mnuClose_Click(object sender, System.EventArgs e)       { MessageBox.Show(&quot;Close file menu clicked&quot;); } </li></ol>

back to the top

Display a Context Menu That Is Specific to the Selected Node
 In Solution Explorer, right-click Form1.cs, and then click View Code.</li>  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 &quot;TextFile&quot;: mnuTextFile.Show(treeView1, p); break; case &quot;File&quot;: mnuFile.Show(treeView1, p); break; }

// Highlight the selected node. treeView1.SelectedNode = m_OldSelectNode; m_OldSelectNode = null; }   } } </li></ol>

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

<div class="references_section">

C# migration of 811399.

Keywords: kbhowto kbhowtomaster kbwindowsforms kbtreeview kbcontmenu kbctrl kbcontrol KB810001

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.