


' It is optimized to true based on empty entry. ' Populate the ContextMenuStrip control with its default items.į("Apples")į("Oranges") ' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)į(("Source: " + tsi.GetType().ToString())) ' Acquire references to the owning control and item.ĭim c As Control = fruitContextMenuStrip.SourceControlĭim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItemį(("Source: " + c.GetType().ToString())) Sub cms_Opening(ByVal sender As Object, ByVal e As ) ' dynamic item addition and dynamic SourceControl ' This event handler is invoked when the ContextMenuStrip ' This is important for correct placement in the z-order. 'Add a button to the form and assign its ContextMenuStrip.ī.Location = New (60, 60)ī.ContextMenuStrip = fruitContextMenuStrip ' Add the ToolStrip control to the Controls collection. Me.ContextMenuStrip = fruitContextMenuStrip ' Assign the ContextMenuStrip to the form's ' Dock the MenuStrip control to the top of the form.įruitToolStripMenuItem.DropDown = fruitContextMenuStrip ' Create a new MenuStrip control and add a ToolStripMenuItem.ĭim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit") ' ToolStripDropDownButton control's DropDown menu.įruitToolStripDropDownButton.DropDown = fruitContextMenuStrip ' Assign the ContextMenuStrip control as the ' Dock the ToolStrip control to the top of the form. Ts.Items.Add(fruitToolStripDropDownButton) ' to the ToolStrip control's Items collections.ĭim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit") ' Create a ToolStripDropDownButton control and add it ' ContextMenuStrip control's Opening event.ĪddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening Private fruitContextMenuStrip As ContextMenuStripįruitContextMenuStrip = New ContextMenuStrip() ' SourceControl determination with reuse. ' It also demonstrates dynamic item addition and dynamic
Vb net menustrip how to#
' This code example demonstrates how to handle the Opening event. It is optimized to true based on empty entry. Populate the ContextMenuStrip control with its default items.į("Apples") į("Oranges") į("Pears") Add custom item (ToolStripDropDownButton or ToolStripMenuItem)į("Source: " + tsi.GetType().ToString()) Clear the ContextMenuStrip control's Items collection.į("Source: " + c.GetType().ToString()) ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem Acquire references to the owning control and item.Ĭontrol c = fruitContextMenuStrip.SourceControl as Control dynamic item addition and dynamic SourceControl This event handler is invoked when the ContextMenuStrip This is important for correct placement in the z-order. Add a button to the form and assign its ContextMenuStrip.ī.Location = new (60, 60) ī.ContextMenuStrip = fruitContextMenuStrip Add the ToolStrip control to the Controls collection. This.ContextMenuStrip = fruitContextMenuStrip Assign the ContextMenuStrip to the form's Dock the MenuStrip control to the top of the form.įruitToolStripMenuItem.DropDown = fruitContextMenuStrip ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit") Create a new MenuStrip control and add a ToolStripMenuItem. ToolStripDropDownButton control's DropDown menu.įruitToolStripDropDownButton.DropDown = fruitContextMenuStrip Assign the ContextMenuStrip control as the Dock the ToolStrip control to the top of the form. ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit")

to the ToolStrip control's Items collections. Create a ToolStripDropDownButton control and add it ContextMenuStrip control's Opening event.įruitContextMenuStrip.Opening += new (cms_Opening) Create a new ContextMenuStrip control.įruitContextMenuStrip = new ContextMenuStrip() Private ContextMenuStrip fruitContextMenuStrip SourceControl determination with reuse. It also demonstrates dynamic item addition and dynamic This code example demonstrates how to handle the Opening event. The Opening event handler examines the ContextMenuStrip.SourceControl and ToolStripItem.OwnerItem properties and adds a ToolStripItem describing the source control. In the example, an Opening event handler populates the menu item collection. The example also shows how to reuse the same ContextMenuStrip for three different controls on the form. The following code example demonstrates how to dynamically add items to a ContextMenuStrip control. You can dynamically populate the menu item collection of a ToolStrip control when the menu opens.
