ToolStripDropDownItem 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
針對按下 ToolStripDropDown、ToolStripDropDownButton 或 ToolStripMenuItem 控制項時顯示 ToolStripSplitButton 的控制項提供基本功能。
public ref class ToolStripDropDownItem abstract : System::Windows::Forms::ToolStripItem
public abstract class ToolStripDropDownItem : System.Windows.Forms.ToolStripItem
type ToolStripDropDownItem = class
inherit ToolStripItem
Public MustInherit Class ToolStripDropDownItem
Inherits ToolStripItem
- 繼承
- 繼承
- 衍生
範例
下列程式碼範例示範如何顯示和隱藏 ToolStripMenuItem 控制項。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ToolStripDropDownItemCS
{
public class Form1 : Form
{
private ToolStrip toolStrip1;
private StatusStrip statusStrip1;
private ToolStripStatusLabel toolStripStatusLabel1;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem menuItem1ToolStripMenuItem;
private ToolStripMenuItem menuItem2ToolStripMenuItem;
private ToolStripMenuItem subItemToolStripMenuItem;
private ToolStripMenuItem subItem2ToolStripMenuItem;
private Button showButton;
private Button hideButton;
private System.ComponentModel.IContainer components = null;
public Form1()
{
InitializeComponent();
this.InitializeToolStripDropDownItems();
}
// This utility method creates and initializes three
// ToolStripDropDownItem controls and adds them
// to the form's ToolStrip control.
private void InitializeToolStripDropDownItems()
{
ToolStripDropDownButton b = new ToolStripDropDownButton("DropDownButton");
b.DropDown = this.contextMenuStrip1;
b.DropDownClosed += new EventHandler(toolStripDropDownItem_DropDownClosed);
b.DropDownItemClicked += new ToolStripItemClickedEventHandler(toolStripDropDownItem_DropDownItemClicked);
b.DropDownOpened += new EventHandler(toolStripDropDownItem_DropDownOpened);
ToolStripMenuItem m = new ToolStripMenuItem("MenuItem");
m.DropDown = this.contextMenuStrip1;
m.DropDownClosed += new EventHandler(toolStripDropDownItem_DropDownClosed);
m.DropDownItemClicked += new ToolStripItemClickedEventHandler(toolStripDropDownItem_DropDownItemClicked);
m.DropDownOpened += new EventHandler(toolStripDropDownItem_DropDownOpened);
ToolStripSplitButton sb = new ToolStripSplitButton("SplitButton");
sb.DropDown = this.contextMenuStrip1;
sb.DropDownClosed += new EventHandler(toolStripDropDownItem_DropDownClosed);
sb.DropDownItemClicked += new ToolStripItemClickedEventHandler(toolStripDropDownItem_DropDownItemClicked);
sb.DropDownOpened += new EventHandler(toolStripDropDownItem_DropDownOpened);
this.toolStrip1.Items.AddRange(new ToolStripItem[] { b, m, sb });
}
// This method handles the DropDownOpened event from a
// ToolStripDropDownItem. It displays the value of the
// item's Text property in the form's StatusStrip control.
void toolStripDropDownItem_DropDownOpened(object sender, EventArgs e)
{
ToolStripDropDownItem item = sender as ToolStripDropDownItem;
string msg = String.Format("Item opened: {0}", item.Text);
this.toolStripStatusLabel1.Text = msg;
}
// This method handles the DropDownItemClicked event from a
// ToolStripDropDownItem. It displays the value of the clicked
// item's Text property in the form's StatusStrip control.
void toolStripDropDownItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string msg = String.Format("Item clicked: {0}", e.ClickedItem.Text);
this.toolStripStatusLabel1.Text = msg;
}
// This method handles the DropDownClosed event from a
// ToolStripDropDownItem. It displays the value of the
// item's Text property in the form's StatusStrip control.
void toolStripDropDownItem_DropDownClosed(object sender, EventArgs e)
{
ToolStripDropDownItem item = sender as ToolStripDropDownItem;
string msg = String.Format("Item closed: {0}", item.Text);
this.toolStripStatusLabel1.Text = msg;
}
// This method shows the drop-down for the first item
// in the form's ToolStrip.
private void showButton_Click(object sender, EventArgs e)
{
ToolStripDropDownItem item = this.toolStrip1.Items[0] as ToolStripDropDownItem;
if (item.HasDropDownItems)
{
item.ShowDropDown();
}
}
// This method hides the drop-down for the first item
// in the form's ToolStrip.
private void hideButton_Click(object sender, EventArgs e)
{
ToolStripDropDownItem item = this.toolStrip1.Items[0] as ToolStripDropDownItem;
item.HideDropDown();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.menuItem1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuItem2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.subItemToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.subItem2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.showButton = new System.Windows.Forms.Button();
this.hideButton = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(292, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1});
this.statusStrip1.Location = new System.Drawing.Point(0, 251);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(292, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(38, 17);
this.toolStripStatusLabel1.Text = "Ready";
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menuItem1ToolStripMenuItem,
this.menuItem2ToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.contextMenuStrip1.Size = new System.Drawing.Size(146, 48);
//
// menuItem1ToolStripMenuItem
//
this.menuItem1ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.subItemToolStripMenuItem});
this.menuItem1ToolStripMenuItem.Name = "menuItem1ToolStripMenuItem";
this.menuItem1ToolStripMenuItem.Size = new System.Drawing.Size(145, 22);
this.menuItem1ToolStripMenuItem.Text = "Menu Item1";
//
// menuItem2ToolStripMenuItem
//
this.menuItem2ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.subItem2ToolStripMenuItem});
this.menuItem2ToolStripMenuItem.Name = "menuItem2ToolStripMenuItem";
this.menuItem2ToolStripMenuItem.Size = new System.Drawing.Size(145, 22);
this.menuItem2ToolStripMenuItem.Text = "Menu Item 2";
//
// subItemToolStripMenuItem
//
this.subItemToolStripMenuItem.Name = "subItemToolStripMenuItem";
this.subItemToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.subItemToolStripMenuItem.Text = "Sub Item";
//
// subItem2ToolStripMenuItem
//
this.subItem2ToolStripMenuItem.Name = "subItem2ToolStripMenuItem";
this.subItem2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.subItem2ToolStripMenuItem.Text = "Sub Item2";
//
// showButton
//
this.showButton.Location = new System.Drawing.Point(12, 180);
this.showButton.Name = "showButton";
this.showButton.Size = new System.Drawing.Size(75, 23);
this.showButton.TabIndex = 2;
this.showButton.Text = "Show";
this.showButton.UseVisualStyleBackColor = true;
this.showButton.Click += new System.EventHandler(this.showButton_Click);
//
// hideButton
//
this.hideButton.Location = new System.Drawing.Point(12, 209);
this.hideButton.Name = "hideButton";
this.hideButton.Size = new System.Drawing.Size(75, 23);
this.hideButton.TabIndex = 3;
this.hideButton.Text = "Hide";
this.hideButton.UseVisualStyleBackColor = true;
this.hideButton.Click += new System.EventHandler(this.hideButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.hideButton);
this.Controls.Add(this.toolStrip1);
this.Controls.Add(this.showButton);
this.Name = "Form1";
this.Text = "Form1";
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private toolStrip1 As ToolStrip
Private statusStrip1 As StatusStrip
Private toolStripStatusLabel1 As ToolStripStatusLabel
Private contextMenuStrip1 As ContextMenuStrip
Private menuItem1ToolStripMenuItem As ToolStripMenuItem
Private menuItem2ToolStripMenuItem As ToolStripMenuItem
Private subItemToolStripMenuItem As ToolStripMenuItem
Private subItem2ToolStripMenuItem As ToolStripMenuItem
Private WithEvents showButton As Button
Private WithEvents hideButton As Button
Private components As System.ComponentModel.IContainer = Nothing
Public Sub New()
InitializeComponent()
Me.InitializeToolStripDropDownItems()
End Sub
' This utility method creates and initializes three
' ToolStripDropDownItem controls and adds them
' to the form's ToolStrip control.
Private Sub InitializeToolStripDropDownItems()
Dim b As New ToolStripDropDownButton("DropDownButton")
b.DropDown = Me.contextMenuStrip1
AddHandler b.DropDownClosed, AddressOf toolStripDropDownItem_DropDownClosed
AddHandler b.DropDownItemClicked, AddressOf toolStripDropDownItem_DropDownItemClicked
AddHandler b.DropDownOpened, AddressOf toolStripDropDownItem_DropDownOpened
Dim m As New ToolStripMenuItem("MenuItem")
m.DropDown = Me.contextMenuStrip1
AddHandler m.DropDownClosed, AddressOf toolStripDropDownItem_DropDownClosed
AddHandler m.DropDownItemClicked, AddressOf toolStripDropDownItem_DropDownItemClicked
AddHandler m.DropDownOpened, AddressOf toolStripDropDownItem_DropDownOpened
Dim sb As New ToolStripSplitButton("SplitButton")
sb.DropDown = Me.contextMenuStrip1
AddHandler sb.DropDownClosed, AddressOf toolStripDropDownItem_DropDownClosed
AddHandler sb.DropDownItemClicked, AddressOf toolStripDropDownItem_DropDownItemClicked
AddHandler sb.DropDownOpened, AddressOf toolStripDropDownItem_DropDownOpened
Me.toolStrip1.Items.AddRange(New ToolStripItem() {b, m, sb})
End Sub
' This method handles the DropDownOpened event from a
' ToolStripDropDownItem. It displays the value of the
' item's Text property in the form's StatusStrip control.
Private Sub toolStripDropDownItem_DropDownOpened(ByVal sender As Object, ByVal e As EventArgs)
Dim item As ToolStripDropDownItem = CType(sender, ToolStripDropDownItem)
Dim msg As String = String.Format("Item opened: {0}", item.Text)
Me.toolStripStatusLabel1.Text = msg
End Sub
' This method handles the DropDownItemClicked event from a
' ToolStripDropDownItem. It displays the value of the clicked
' item's Text property in the form's StatusStrip control.
Private Sub toolStripDropDownItem_DropDownItemClicked( _
ByVal sender As Object, _
ByVal e As ToolStripItemClickedEventArgs)
Dim msg As String = String.Format("Item clicked: {0}", e.ClickedItem.Text)
Me.toolStripStatusLabel1.Text = msg
End Sub
' This method handles the DropDownClosed event from a
' ToolStripDropDownItem. It displays the value of the
' item's Text property in the form's StatusStrip control.
Private Sub toolStripDropDownItem_DropDownClosed(ByVal sender As Object, ByVal e As EventArgs)
Dim item As ToolStripDropDownItem = CType(sender, ToolStripDropDownItem)
Dim msg As String = String.Format("Item closed: {0}", item.Text)
Me.toolStripStatusLabel1.Text = msg
End Sub
' This method shows the drop-down for the first item
' in the form's ToolStrip.
Private Sub showButton_Click( _
ByVal sender As Object, _
ByVal e As EventArgs) _
Handles showButton.Click
Dim item As ToolStripDropDownItem = CType(Me.toolStrip1.Items(0), ToolStripDropDownItem)
If item.HasDropDownItems Then
item.ShowDropDown()
End If
End Sub
' This method hides the drop-down for the first item
' in the form's ToolStrip.
Private Sub hideButton_Click( _
ByVal sender As Object, _
ByVal e As EventArgs) _
Handles hideButton.Click
Dim item As ToolStripDropDownItem = CType(Me.toolStrip1.Items(0), ToolStripDropDownItem)
item.HideDropDown()
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing AndAlso (components IsNot Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.toolStrip1 = New System.Windows.Forms.ToolStrip()
Me.statusStrip1 = New System.Windows.Forms.StatusStrip()
Me.toolStripStatusLabel1 = New System.Windows.Forms.ToolStripStatusLabel()
Me.contextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
Me.menuItem1ToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.menuItem2ToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.subItemToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.subItem2ToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.showButton = New System.Windows.Forms.Button()
Me.hideButton = New System.Windows.Forms.Button()
Me.statusStrip1.SuspendLayout()
Me.contextMenuStrip1.SuspendLayout()
Me.SuspendLayout()
'
' toolStrip1
'
Me.toolStrip1.Location = New System.Drawing.Point(0, 0)
Me.toolStrip1.Name = "toolStrip1"
Me.toolStrip1.Size = New System.Drawing.Size(292, 25)
Me.toolStrip1.TabIndex = 0
Me.toolStrip1.Text = "toolStrip1"
'
' statusStrip1
'
Me.statusStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.toolStripStatusLabel1})
Me.statusStrip1.Location = New System.Drawing.Point(0, 251)
Me.statusStrip1.Name = "statusStrip1"
Me.statusStrip1.Size = New System.Drawing.Size(292, 22)
Me.statusStrip1.TabIndex = 1
Me.statusStrip1.Text = "statusStrip1"
'
' toolStripStatusLabel1
'
Me.toolStripStatusLabel1.Name = "toolStripStatusLabel1"
Me.toolStripStatusLabel1.Size = New System.Drawing.Size(38, 17)
Me.toolStripStatusLabel1.Text = "Ready"
'
' contextMenuStrip1
'
Me.contextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.menuItem1ToolStripMenuItem, Me.menuItem2ToolStripMenuItem})
Me.contextMenuStrip1.Name = "contextMenuStrip1"
Me.contextMenuStrip1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.contextMenuStrip1.Size = New System.Drawing.Size(146, 48)
'
' menuItem1ToolStripMenuItem
'
Me.menuItem1ToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.subItemToolStripMenuItem})
Me.menuItem1ToolStripMenuItem.Name = "menuItem1ToolStripMenuItem"
Me.menuItem1ToolStripMenuItem.Size = New System.Drawing.Size(145, 22)
Me.menuItem1ToolStripMenuItem.Text = "Menu Item1"
'
' menuItem2ToolStripMenuItem
'
Me.menuItem2ToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.subItem2ToolStripMenuItem})
Me.menuItem2ToolStripMenuItem.Name = "menuItem2ToolStripMenuItem"
Me.menuItem2ToolStripMenuItem.Size = New System.Drawing.Size(145, 22)
Me.menuItem2ToolStripMenuItem.Text = "Menu Item 2"
'
' subItemToolStripMenuItem
'
Me.subItemToolStripMenuItem.Name = "subItemToolStripMenuItem"
Me.subItemToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
Me.subItemToolStripMenuItem.Text = "Sub Item"
'
' subItem2ToolStripMenuItem
'
Me.subItem2ToolStripMenuItem.Name = "subItem2ToolStripMenuItem"
Me.subItem2ToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
Me.subItem2ToolStripMenuItem.Text = "Sub Item2"
'
' showButton
'
Me.showButton.Location = New System.Drawing.Point(12, 180)
Me.showButton.Name = "showButton"
Me.showButton.Size = New System.Drawing.Size(75, 23)
Me.showButton.TabIndex = 2
Me.showButton.Text = "Show"
Me.showButton.UseVisualStyleBackColor = True
'
' hideButton
'
Me.hideButton.Location = New System.Drawing.Point(12, 209)
Me.hideButton.Name = "hideButton"
Me.hideButton.Size = New System.Drawing.Size(75, 23)
Me.hideButton.TabIndex = 3
Me.hideButton.Text = "Hide"
Me.hideButton.UseVisualStyleBackColor = True
'
' Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(statusStrip1)
Me.Controls.Add(hideButton)
Me.Controls.Add(toolStrip1)
Me.Controls.Add(showButton)
Me.Name = "Form1"
Me.Text = "Form1"
Me.statusStrip1.ResumeLayout(False)
Me.statusStrip1.PerformLayout()
Me.contextMenuStrip1.ResumeLayout(False)
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
End Class
Public Class Program
' The main entry point for the application.
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
End Sub
End Class
備註
ToolStripDropDownItem是 、 ToolStripDropDownButton 和 ToolStripSplitButton 的 ToolStripMenuItem 抽象基類,可以直接裝載專案,或裝載下拉式容器中的其他專案。 若要這樣做, DropDown 您可以將 屬性設定為 ToolStripDropDown ,並設定 Items 的 ToolStripDropDown 屬性。 直接透過 DropDownItems 屬性存取這些下拉式清單專案。
建構函式
ToolStripDropDownItem() |
初始化 ToolStripDropDownItem 類別的新執行個體。 |
ToolStripDropDownItem(String, Image, EventHandler) |
使用指定的顯示文字、影像和按一下下拉式控制項後所要採取的動作,初始化 ToolStripDropDownItem 類別的新執行個體。 |
ToolStripDropDownItem(String, Image, EventHandler, String) |
使用指定的顯示文字、影像、按一下下拉式控制項後所要採取的動作,以及控制項名稱,初始化 ToolStripDropDownItem 類別的新執行個體。 |
ToolStripDropDownItem(String, Image, ToolStripItem[]) |
使用指定的顯示文字、影像和下拉式控制項所包含的 ToolStripDropDownItem 集合,初始化 ToolStripItem 類別的新執行個體。 |
屬性
方法
事件
明確介面實作
IDropTarget.OnDragDrop(DragEventArgs) |
引發 DragDrop 事件。 (繼承來源 ToolStripItem) |
IDropTarget.OnDragEnter(DragEventArgs) |
引發 DragEnter 事件。 (繼承來源 ToolStripItem) |
IDropTarget.OnDragLeave(EventArgs) |
引發 DragLeave 事件。 (繼承來源 ToolStripItem) |
IDropTarget.OnDragOver(DragEventArgs) |
引發 |