ToolBarButton 类

定义

表示一个 Windows 工具栏按钮。Represents a Windows toolbar button.

此类在 .NET Core 3.1 及更高版本中不可用。This class is not available in .NET Core 3.1 and later versions. ToolStripButton改用来替换和扩展 ToolBarButton 控件。Use ToolStripButton instead, which replaces and extends the ToolBarButton control.

public ref class ToolBarButton : System::ComponentModel::Component
public class ToolBarButton : System.ComponentModel.Component
type ToolBarButton = class
    inherit Component
Public Class ToolBarButton
Inherits Component
继承

示例

下面的代码示例创建一个 ToolBar 和三个 ToolBarButton 控件。The following code example creates a ToolBar and three ToolBarButton controls. 工具栏按钮将分配给按钮集合,集合将分配给工具栏,并将工具栏添加到窗体中。The toolbar buttons are assigned to the button collection, the collection is assigned to the toolbar, and the toolbar is added to the form. ButtonClick 工具栏的事件上, Button 计算的属性, ToolBarButtonClickEventArgs 并打开适当的对话框。On the ButtonClick event of the toolbar, the Button property of the ToolBarButtonClickEventArgs is evaluated and the appropriate dialog box opened. 此代码要求 Form 已创建、、 OpenFileDialog SaveFileDialog 和的 PrintDialogThis code requires that a Form, an OpenFileDialog, a SaveFileDialog, and a PrintDialog have all been created.

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 及更高版本中不可用。This class is not available in .NET Core 3.1 and later versions. 请改用 Use instead.

ToolBarButton 控件是控件的父级 ToolBarToolBarButton controls are parented by ToolBar controls. 创建工具栏按钮后要设置的通用属性为 TextImageIndexCommon properties to set once the toolbar button has been created are Text and ImageIndex. 设置 Text 按钮的属性,以在图像的下方或右侧显示文本。Set the Text property of the button to display text beneath or to the right of the image. 若要通过创建将图像分配给按钮 ImageList ,请将其分配给 ImageList 工具栏的属性,然后将图像索引值分配给 ImageIndex 按钮的属性。To assign images to the buttons by creating an ImageList, assigning it to the ImageList property of the toolbar; then assign the image index value to the ImageIndex property of the button.

若要更改分配给工具栏的工具栏按钮的外观,请设置 Appearance 父 toolbar 控件的属性。To change the appearance of the toolbar buttons assigned to the toolbar, set the Appearance property of the parent toolbar control. ToolBarAppearance.Flat外观为按钮提供平面外观。The ToolBarAppearance.Flat appearance gives the buttons a flat appearance. 当鼠标指针移到按钮上时,其外观将变为三维。As the mouse pointer moves over the buttons, their appearance changes to three-dimensional. 当按钮具有平面外观时,按钮分隔符显示为线条,而不是按钮之间的空格。Button separators appear as lines rather than spaces between the buttons when the buttons have a flat appearance. 如果将 Appearance 属性设置为 ToolBarAppearance.Normal ,则按钮显示为凸起和三维,分隔符显示为按钮之间的间隔。If the Appearance property is set to ToolBarAppearance.Normal, the buttons appear raised and three-dimensional, and the separators appear as a gap between the buttons.

ContextMenu如果将 Style 属性设置为,则可以为该按钮指定 ToolBarButtonStyle.DropDownYou can assign a ContextMenu to a button if the Style property is set to ToolBarButtonStyle.DropDown. 单击该按钮时,将显示分配的菜单。When the button is clicked, the assigned menu is displayed.

若要创建 ToolBarButton 要在上显示的控件的集合 ToolBar ,请使用属性的方法分别添加按钮 Add ButtonsTo create a collection of ToolBarButton controls to display on a ToolBar, add the buttons individually by using the Add method of the Buttons property. 或者,您可以使用方法添加多个工具栏按钮 AddRangeAlternatively, you can add several toolbar buttons using the AddRange method.

构造函数

ToolBarButton()

初始化 ToolBarButton 类的新实例。Initializes a new instance of the ToolBarButton class.

ToolBarButton(String)

初始化 ToolBarButton 类的新实例,并在按钮上显示分配的文本。Initializes a new instance of the ToolBarButton class and displays the assigned text on the button.

属性

CanRaiseEvents

获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(继承自 Component)
Container

获取包含 IContainerComponentGets the IContainer that contains the Component.

(继承自 Component)
DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(继承自 Component)
DropDownMenu

获取或设置要显示在下拉工具栏按钮中的菜单。Gets or sets the menu to be displayed in the drop-down toolbar button.

Enabled

获取或设置一个值,该值指示是否启用此按钮。Gets or sets a value indicating whether the button is enabled.

Events

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(继承自 Component)
ImageIndex

获取或设置分配给按钮的图像的索引值。Gets or sets the index value of the image assigned to the button.

ImageKey

获取或设置分配给此按钮的图像的名称。Gets or sets the name of the image assigned to the button.

Name

按钮的名称。The name of the button.

Parent

获取向其分配工具栏按钮的工具栏控件。Gets the toolbar control that the toolbar button is assigned to.

PartialPush

获取或设置一个值,该值指示切换式工具栏按钮是否为部分按下状态。Gets or sets a value indicating whether a toggle-style toolbar button is partially pushed.

Pushed

获取或设置一个值,该值指示切换式工具栏按钮当前是否处于按下状态。Gets or sets a value indicating whether a toggle-style toolbar button is currently in the pushed state.

Rectangle

获取工具栏按钮的边框。Gets the bounding rectangle for a toolbar button.

Site

获取或设置 ComponentISiteGets or sets the ISite of the Component.

(继承自 Component)
Style

获取或设置工具栏按钮的样式。Gets or sets the style of the toolbar button.

Tag

获取或设置包含有关工具栏按钮的数据的对象。Gets or sets the object that contains data about the toolbar button.

Text

获取或设置显示在工具栏按钮上的文本。Gets or sets the text displayed on the toolbar button.

ToolTipText

获取或设置显示为该按钮的工具提示的文本。Gets or sets the text that appears as a ToolTip for the button.

Visible

获取或设置一个值,该值指示工具栏按钮是否可见。Gets or sets a value indicating whether the toolbar button is visible.

方法

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放由 Component 使用的所有资源。Releases all resources used by the Component.

(继承自 Component)
Dispose(Boolean)

释放由 ToolBarButton 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the ToolBarButton and optionally releases the managed resources.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(继承自 Component)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
ToString()

返回表示 ToolBarButton 控件的字符串。Returns a string that represents the ToolBarButton control.

事件

Disposed

在通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(继承自 Component)

适用于

另请参阅