ToolBarButton 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个 Windows 工具栏按钮。
此类在 .NET Core 3.1 及更高版本中不可用。 请改用 ToolStripButton,它替换和扩展了 ToolBarButton 控件。
public ref class ToolBarButton : System::ComponentModel::Componentpublic class ToolBarButton : System.ComponentModel.Componenttype ToolBarButton = class
    inherit ComponentPublic Class ToolBarButton
Inherits Component- 继承
示例
下面的代码示例创建 一个 和三个 ToolBarToolBarButton 控件。 工具栏按钮分配给按钮集合,集合分配给工具栏,工具栏添加到窗体。 在 ButtonClick 工具栏的 事件中 Button ,计算 的 ToolBarButtonClickEventArgs 属性并打开相应的对话框。 此代码要求 Form已创建 、 OpenFileDialog、 SaveFileDialog和 PrintDialog 。
public:
   void InitializeMyToolBar()
   {
      // Create and initialize the ToolBar and ToolBarButton controls.
      toolBar1 = gcnew ToolBar;
      ToolBarButton^ toolBarButton1 = gcnew ToolBarButton;
      ToolBarButton^ toolBarButton2 = gcnew ToolBarButton;
      ToolBarButton^ toolBarButton3 = gcnew ToolBarButton;
      
      // Set the Text properties of the ToolBarButton controls.
      toolBarButton1->Text = "Open";
      toolBarButton2->Text = "Save";
      toolBarButton3->Text = "Print";
      
      // Add the ToolBarButton controls to the ToolBar.
      toolBar1->Buttons->Add( toolBarButton1 );
      toolBar1->Buttons->Add( toolBarButton2 );
      toolBar1->Buttons->Add( toolBarButton3 );
      
      // Add the event-handler delegate.
      toolBar1->ButtonClick += gcnew ToolBarButtonClickEventHandler(
         this, &Form1::toolBar1_ButtonClick );
      
      // Add the ToolBar to the Form.
      Controls->Add( toolBar1 );
   }
private:
   void toolBar1_ButtonClick(
      Object^ sender,
      ToolBarButtonClickEventArgs^ e )
   {
      // Evaluate the Button property to determine which button was clicked.
      switch ( toolBar1->Buttons->IndexOf( e->Button ) )
      {
         case 0:
            openFileDialog1->ShowDialog();
            // Insert code to open the file.
            break;
         case 1:
            saveFileDialog1->ShowDialog();
            // Insert code to save the file.
            break;
         case 2:
            printDialog1->ShowDialog();
            // Insert code to print the file.    
            break;
      }
   }
public void InitializeMyToolBar()
 {
    // Create and initialize the ToolBar and ToolBarButton controls.
    toolBar1 = new ToolBar();
    ToolBarButton toolBarButton1 = new ToolBarButton();
    ToolBarButton toolBarButton2 = new ToolBarButton();
    ToolBarButton toolBarButton3 = new ToolBarButton();
 
    // Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open";
    toolBarButton2.Text = "Save";
    toolBarButton3.Text = "Print";
 
    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1);
    toolBar1.Buttons.Add(toolBarButton2);
    toolBar1.Buttons.Add(toolBarButton3);
    
    // Add the event-handler delegate.
    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (
       this.toolBar1_ButtonClick);
    
    // Add the ToolBar to the Form.
    Controls.Add(toolBar1);
 }
 
 private void toolBar1_ButtonClick (
                         Object sender, 
                         ToolBarButtonClickEventArgs e)
 {
   // Evaluate the Button property to determine which button was clicked.
   switch(toolBar1.Buttons.IndexOf(e.Button))
   {
      case 0:
         openFileDialog1.ShowDialog();
         // Insert code to open the file.
         break; 
      case 1:
         saveFileDialog1.ShowDialog();
         // Insert code to save the file.
         break; 
      case 2:
         printDialog1.ShowDialog();
         // Insert code to print the file.    
         break; 
    }
 }
Public Sub InitializeMyToolBar()
    ' Create and initialize the ToolBar and ToolBarButton controls.
    Dim toolBar1 As New ToolBar()
    Dim toolBarButton1 As New ToolBarButton()
    Dim toolBarButton2 As New ToolBarButton()
    Dim toolBarButton3 As New ToolBarButton()
    
    ' Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open"
    toolBarButton2.Text = "Save"
    toolBarButton3.Text = "Print"
    
    ' Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1)
    toolBar1.Buttons.Add(toolBarButton2)
    toolBar1.Buttons.Add(toolBarButton3)
    
    ' Add the event-handler delegate.
    AddHandler toolBar1.ButtonClick, AddressOf Me.toolBar1_ButtonClick
    
    ' Add the ToolBar to the Form.
    Controls.Add(toolBar1)
End Sub    
Private Sub toolBar1_ButtonClick(ByVal sender As Object, _
ByVal e As ToolBarButtonClickEventArgs)
    ' Evaluate the Button property to determine which button was clicked.
    Select Case toolBar1.Buttons.IndexOf(e.Button)
        Case 0
            openFileDialog1.ShowDialog()
            ' Insert code to open the file.
        Case 1
            saveFileDialog1.ShowDialog()
            ' Insert code to save the file.
        Case 2
            printDialog1.ShowDialog()
            ' Insert code to print the file.
    End Select
End Sub
注解
此类在 .NET Core 3.1 及更高版本中不可用。 请改用 ToolStripButton。
ToolBarButton 控件以 控件为父级 ToolBar 。 创建工具栏按钮后要设置的常见属性是 Text 和 ImageIndex。 Text将按钮的 属性设置为在图像下方或右侧显示文本。 若要通过创建 ImageList为按钮分配图像,请将其分配给 ImageList 工具栏的 属性;然后将图像索引值分配给 ImageIndex 按钮的 属性。
若要更改分配给工具栏的工具栏按钮的外观,请设置 Appearance 父工具栏控件的 属性。 外观 ToolBarAppearance.Flat 使按钮具有平面外观。 当鼠标指针在按钮上移动时,它们的外观将变为三维。 当按钮具有平面外观时,按钮分隔符显示为线条而不是按钮之间的空格。 如果 属性 Appearance 设置为 ToolBarAppearance.Normal,则按钮显示为凸起和三维,分隔符显示为按钮之间的间隙。
如果 Style 属性设置为 ToolBarButtonStyle.DropDown,则可以将 分配给ContextMenu按钮。 单击按钮时,将显示分配的菜单。
若要创建要显示在 上的ToolBar控件集合ToolBarButton,请使用 Add 属性的 Buttons 方法单独添加按钮。 或者,可以使用 方法添加多个工具栏按钮 AddRange 。
构造函数
| ToolBarButton() | 初始化 ToolBarButton 类的新实例。 | 
| ToolBarButton(String) | 初始化 ToolBarButton 类的新实例,并在按钮上显示分配的文本。 | 
属性
| CanRaiseEvents | 获取一个指示组件是否可以引发事件的值。(继承自 Component) | 
| Container | 获取包含 IContainer 的 Component。(继承自 Component) | 
| DesignMode | 获取一个值,用以指示 Component 当前是否处于设计模式。(继承自 Component) | 
| DropDownMenu | 获取或设置要显示在下拉工具栏按钮中的菜单。 | 
| Enabled | 获取或设置一个值,该值指示是否启用此按钮。 | 
| Events | 获取附加到此 Component 的事件处理程序的列表。(继承自 Component) | 
| ImageIndex | 获取或设置分配给按钮的图像的索引值。 | 
| ImageKey | 获取或设置分配给此按钮的图像的名称。 | 
| Name | 按钮的名称。 | 
| Parent | 获取向其分配工具栏按钮的工具栏控件。 | 
| PartialPush | 获取或设置一个值,该值指示切换式工具栏按钮是否为部分按下状态。 | 
| Pushed | 获取或设置一个值,该值指示切换式工具栏按钮当前是否处于按下状态。 | 
| Rectangle | 获取工具栏按钮的边框。 | 
| Site | (继承自 Component) | 
| Style | 获取或设置工具栏按钮的样式。 | 
| Tag | 获取或设置包含有关工具栏按钮的数据的对象。 | 
| Text | 获取或设置显示在工具栏按钮上的文本。 | 
| ToolTipText | 获取或设置显示为该按钮的工具提示的文本。 | 
| Visible | 获取或设置一个值,该值指示工具栏按钮是否可见。 | 
方法
| CreateObjRef(Type) | 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject) | 
| Dispose() | 释放由 Component 使用的所有资源。(继承自 Component) | 
| Dispose(Boolean) | 释放由 ToolBarButton 占用的非托管资源,还可以另外再释放托管资源。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetLifetimeService() | 
		已过时.
	 检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject) | 
| GetService(Type) | 返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。(继承自 Component) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| InitializeLifetimeService() | 
		已过时.
	 获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| MemberwiseClone(Boolean) | 创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject) | 
| ToString() | 返回表示 ToolBarButton 控件的字符串。 | 
事件
| Disposed | 在通过调用 Dispose() 方法释放组件时发生。(继承自 Component) |