Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-associate-a-contextmenustrip-with-a-control.md
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.2 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Associate a ContextMenuStrip with a Control 03/30/2017
csharp
vb
context menus [Windows Forms], relating
ContextMenuStrips [Windows Forms], associating with controls
context menus [Windows Forms], associating with controls
ContextMenuStrips [Windows Forms], relating
6fc40a42-5d69-427f-aa30-0a146193226b

How to: Associate a ContextMenuStrip with a Control

After creating your controls and shortcut menus, use the following procedures to display a given shortcut menu when the user right-clicks the control. These procedures associate a xref:System.Windows.Forms.ContextMenuStrip with a Windows Form and with a xref:System.Windows.Forms.ToolStrip control.

To associate a ContextMenuStrip with a Windows Form

  1. Set the xref:System.Windows.Forms.Control.ContextMenuStrip%2A property to the name of the associated xref:System.Windows.Forms.ContextMenuStrip.

To associate a ContextMenuStrip with a ToolStrip control

  1. Set the control's xref:System.Windows.Forms.Control.ContextMenuStrip%2A property to the name of the associated xref:System.Windows.Forms.ContextMenuStrip.

Example

The following code example creates a Windows Form and a xref:System.Windows.Forms.ToolStrip, and associates a different xref:System.Windows.Forms.ContextMenuStrip control with each of them.

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

Compiling the Code

This example requires:

  • References to the System, System.Data, System.Drawing and System.Windows.Forms assemblies.

See also