Udostępnij za pośrednictwem


How to: Attach a Shortcut Menu to a TreeNode Using the Designer

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

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To associate a shortcut menu with a TreeNode at design time

  1. Add a TreeView control to your form, and then add nodes to the TreeView as needed. For more information, see How to: Add and Remove Nodes with the Windows Forms TreeView Control.

  2. Add a ContextMenuStrip component to your form, and then add menu items to the shortcut menu that represent node-level operations you wish to make available at run time. For more information, see How to: Add Menu Items to a ContextMenuStrip.

  3. Reopen the TreeNodeEditor dialog box for the TreeView control, select the node to edit, and set its ContextMenuStrip property to the shortcut menu that you added.

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

    Additionally, you will want to write code to handle the Click events for these menu items.

See Also

Reference

TreeView Control Overview (Windows Forms)

Other Resources

TreeView Control (Windows Forms)

ContextMenuStrip Control