Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 2.48 KB

how-to-add-toolstrip-items-dynamically.md

File metadata and controls

43 lines (33 loc) · 2.48 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Add ToolStrip Items Dynamically
Learn how to dynamically add ToolStrip items to a ContextMenuStrip control and reuse the same control for several different controls on the form.
03/30/2017
csharp
vb
ContextMenuStrip control [Windows Forms]
toolbars [Windows Forms], adding items dynamically
ToolStrip control [Windows Forms]
0e8dea56-5f46-408b-914d-7e360341a234

How to: Add ToolStrip Items Dynamically

You can dynamically populate the menu item collection of a xref:System.Windows.Forms.ToolStrip control when the menu opens.

Example

The following code example demonstrates how to dynamically add items to a xref:System.Windows.Forms.ContextMenuStrip control. The example also shows how to reuse the same xref:System.Windows.Forms.ContextMenuStrip for three different controls on the form.

In the example, an xref:System.Windows.Forms.ToolStripDropDown.Opening event handler populates the menu item collection. The xref:System.Windows.Forms.ToolStripDropDown.Opening event handler examines the xref:System.Windows.Forms.ContextMenuStrip.SourceControl%2A?displayProperty=nameWithType and xref:System.Windows.Forms.ToolStripItem.OwnerItem%2A?displayProperty=nameWithType properties and adds a xref:System.Windows.Forms.ToolStripItem describing the source control.

[!code-csharpSystem.Windows.Forms.ToolStrip.Misc#1] [!code-vbSystem.Windows.Forms.ToolStrip.Misc#1]
[!code-csharpSystem.Windows.Forms.ToolStrip.Misc#40] [!code-vbSystem.Windows.Forms.ToolStrip.Misc#40]

Compiling the Code

This example requires:

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

See also

  • xref:System.Windows.Forms.ContextMenuStrip
  • xref:System.Windows.Forms.MenuStrip
  • xref:System.Windows.Forms.ToolStrip
  • xref:System.Windows.Forms.ToolStripItem
  • xref:System.Windows.Forms.ToolStripMenuItem
  • xref:System.Windows.Forms.ToolStripDropDownButton
  • ToolStrip Control