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 SaveFileDialogPrintDialogThis 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 父工具列控制項的屬性。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

取得工具列按鈕的週框 (Bounding 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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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)

適用於

另請參閱