Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-attach-a-shortcut-menu-to-a-treeview-node.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

2.9 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Attach a ShortCut Menu to a TreeView Node 03/30/2017
csharp
vb
cpp
shortcut menus [Windows Forms], adding to TreeView controls
TreeView control [Windows Forms], adding shortcut menus
tree nodes in TreeView control [Windows Forms], shortcut menus
a23c6752-fd8f-44ad-b781-bab37962fc7c

How to: Attach a ShortCut Menu to a TreeView Node

The Windows Forms xref:System.Windows.Forms.TreeView control displays a hierarchy of nodes, similar to the files and folders displayed in the left pane of Windows Explorer. By setting the xref:System.Windows.Forms.Control.ContextMenuStrip%2A property, you can provide context-sensitive operations to the user when they right-click the xref:System.Windows.Forms.TreeView control. By associating a xref:System.Windows.Forms.ContextMenuStrip component with individual xref:System.Windows.Forms.TreeNode items, you can add a customized level of shortcut menu functionality to your xref:System.Windows.Forms.TreeView controls.

To associate a shortcut menu with a TreeNode programmatically

  1. Instantiate a xref:System.Windows.Forms.TreeView control with the appropriate property settings, create a root xref:System.Windows.Forms.TreeNode, and then add subnodes.

  2. Instantiate a xref:System.Windows.Forms.ContextMenuStrip component, and then add a xref:System.Windows.Forms.ToolStripMenuItem for each operation you want to make available at run time.

  3. Set the xref:System.Windows.Forms.TreeNode.ContextMenuStrip%2A property of the appropriate xref:System.Windows.Forms.TreeNode to the shortcut menu you create.

  4. When this property is set, the shortcut menu will be displayed when you right-click the node.

The following code example creates a basic xref:System.Windows.Forms.TreeView and xref:System.Windows.Forms.ContextMenuStrip associated with the root xref:System.Windows.Forms.TreeNode of the xref:System.Windows.Forms.TreeView. You will need to customize the menu choices to those that fit the xref:System.Windows.Forms.TreeView you are developing. Additionally, you will want to write code to handle the xref:System.Windows.Forms.ToolStripItem.Click events for these menu items.

[!code-cppSystem.Windows.Forms.TreeNodeContextMenuStrip#1] [!code-csharpSystem.Windows.Forms.TreeNodeContextMenuStrip#1] [!code-vbSystem.Windows.Forms.TreeNodeContextMenuStrip#1]

See also