如何:动态添加 ToolStrip 项
在打开菜单时,可以动态填充 ToolStrip 控件的菜单项集合。
示例
下面的代码示例演示如何向 ContextMenuStrip 控件动态添加项。 该示例还演示了如何对窗体中的三个不同控件重用同一 ContextMenuStrip。
在示例中,Opening 事件处理程序将填充菜单项集合。 Opening 事件处理程序可以检查 ContextMenuStrip.SourceControl 和 ToolStripItem.OwnerItem 属性,并添加描述源控件的 ToolStripItem。
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing
...
' This code example demonstrates how to handle the Opening event.
' It also demonstrates dynamic item addition and dynamic
' SourceControl determination with reuse.
Class Form3
Inherits Form
' Declare the ContextMenuStrip control.
Private fruitContextMenuStrip As ContextMenuStrip
Public Sub New()
' Create a new ContextMenuStrip control.
fruitContextMenuStrip = New ContextMenuStrip()
' Attach an event handler for the
' ContextMenuStrip control's Opening event.
AddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening
' Create a new ToolStrip control.
Dim ts As New ToolStrip()
' Create a ToolStripDropDownButton control and add it
' to the ToolStrip control's Items collections.
Dim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit")
ts.Items.Add(fruitToolStripDropDownButton)
' Dock the ToolStrip control to the top of the form.
ts.Dock = DockStyle.Top
' Assign the ContextMenuStrip control as the
' ToolStripDropDownButton control's DropDown menu.
fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip
' Create a new MenuStrip control and add a ToolStripMenuItem.
Dim ms As New MenuStrip()
Dim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit")
ms.Items.Add(fruitToolStripMenuItem)
' Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top
' Assign the MenuStrip control as the
' ToolStripMenuItem's DropDown menu.
fruitToolStripMenuItem.DropDown = fruitContextMenuStrip
' Assign the ContextMenuStrip to the form's
' ContextMenuStrip property.
Me.ContextMenuStrip = fruitContextMenuStrip
' Add the ToolStrip control to the Controls collection.
Me.Controls.Add(ts)
'Add a button to the form and assign its ContextMenuStrip.
Dim b As New Button()
b.Location = New System.Drawing.Point(60, 60)
Me.Controls.Add(b)
b.ContextMenuStrip = fruitContextMenuStrip
' Add the MenuStrip control last.
' This is important for correct placement in the z-order.
Me.Controls.Add(ms)
End Sub
' This event handler is invoked when the ContextMenuStrip
' control's Opening event is raised. It demonstrates
' dynamic item addition and dynamic SourceControl
' determination with reuse.
Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
' Acquire references to the owning control and item.
Dim c As Control = fruitContextMenuStrip.SourceControl
Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem
' Clear the ContextMenuStrip control's
' Items collection.
fruitContextMenuStrip.Items.Clear()
' Check the source control first.
If (c IsNot Nothing) Then
' Add custom item (Form)
fruitContextMenuStrip.Items.Add(("Source: " + c.GetType().ToString()))
ElseIf (tsi IsNot Nothing) Then
' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
fruitContextMenuStrip.Items.Add(("Source: " + tsi.GetType().ToString()))
End If
' Populate the ContextMenuStrip control with its default items.
fruitContextMenuStrip.Items.Add("-")
fruitContextMenuStrip.Items.Add("Apples")
fruitContextMenuStrip.Items.Add("Oranges")
fruitContextMenuStrip.Items.Add("Pears")
' Set Cancel to false.
' It is optimized to true based on empty entry.
e.Cancel = False
End Sub
End Class
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
...
// This code example demonstrates how to handle the Opening event.
// It also demonstrates dynamic item addition and dynamic
// SourceControl determination with reuse.
class Form3 : Form
{
// Declare the ContextMenuStrip control.
private ContextMenuStrip fruitContextMenuStrip;
public Form3()
{
// Create a new ContextMenuStrip control.
fruitContextMenuStrip = new ContextMenuStrip();
// Attach an event handler for the
// ContextMenuStrip control's Opening event.
fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);
// Create a new ToolStrip control.
ToolStrip ts = new ToolStrip();
// Create a ToolStripDropDownButton control and add it
// to the ToolStrip control's Items collections.
ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit");
ts.Items.Add(fruitToolStripDropDownButton);
// Dock the ToolStrip control to the top of the form.
ts.Dock = DockStyle.Top;
// Assign the ContextMenuStrip control as the
// ToolStripDropDownButton control's DropDown menu.
fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip;
// Create a new MenuStrip control and add a ToolStripMenuItem.
MenuStrip ms = new MenuStrip();
ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");
ms.Items.Add(fruitToolStripMenuItem);
// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;
// Assign the MenuStrip control as the
// ToolStripMenuItem's DropDown menu.
fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;
// Assign the ContextMenuStrip to the form's
// ContextMenuStrip property.
this.ContextMenuStrip = fruitContextMenuStrip;
// Add the ToolStrip control to the Controls collection.
this.Controls.Add(ts);
//Add a button to the form and assign its ContextMenuStrip.
Button b = new Button();
b.Location = new System.Drawing.Point(60, 60);
this.Controls.Add(b);
b.ContextMenuStrip = fruitContextMenuStrip;
// Add the MenuStrip control last.
// This is important for correct placement in the z-order.
this.Controls.Add(ms);
}
// This event handler is invoked when the ContextMenuStrip
// control's Opening event is raised. It demonstrates
// dynamic item addition and dynamic SourceControl
// determination with reuse.
void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
// Acquire references to the owning control and item.
Control c = fruitContextMenuStrip.SourceControl as Control;
ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem;
// Clear the ContextMenuStrip control's Items collection.
fruitContextMenuStrip.Items.Clear();
// Check the source control first.
if (c != null)
{
// Add custom item (Form)
fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString());
}
else if (tsi != null)
{
// Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
fruitContextMenuStrip.Items.Add("Source: " + tsi.GetType().ToString());
}
// Populate the ContextMenuStrip control with its default items.
fruitContextMenuStrip.Items.Add("-");
fruitContextMenuStrip.Items.Add("Apples");
fruitContextMenuStrip.Items.Add("Oranges");
fruitContextMenuStrip.Items.Add("Pears");
// Set Cancel to false.
// It is optimized to true based on empty entry.
e.Cancel = false;
}
}
编译代码
此示例需要:
- 对 System.Drawing 和 System.Windows.Forms 程序集的引用。
有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic) 或在命令行上使用 csc.exe 生成。 也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。 有关更多信息,请参见 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例.