ToolBarButton 類別

定義

表示 Windows 工具列按鈕。

此類別在 .NET Core 3.1 和更新版本中無法使用。 請改用 ToolStripButton ,以取代和擴充 ToolBarButton 控制項。

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

範例

下列程式碼範例會建立 和三 ToolBarButtonToolBar 控制項。 工具列按鈕會指派給按鈕集合、將集合指派給工具列,並將工具列新增至表單。 在 ButtonClick 工具列的事件上, Button 會評估 的 ToolBarButtonClickEventArgs 屬性,並開啟適當的對話方塊。 此程式碼需要 Form 已建立 、、 OpenFileDialogSaveFileDialogPrintDialog

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 父代。 建立工具列按鈕後要設定的通用屬性為 TextImageIndexText設定按鈕的 屬性,以顯示影像下方或右邊的文字。 若要藉由建立 ImageList ,將影像指派給 ImageList 工具列的 屬性,然後將影像索引值指派給 ImageIndex 按鈕的 屬性。

若要變更指派給工具列的工具列按鈕外觀,請設定 Appearance 父工具列控制項的 屬性。 外觀 ToolBarAppearance.Flat 可讓按鈕呈現一般外觀。 當滑鼠指標移至按鈕上方時,其外觀會變更為三維。 按鈕分隔符號會在按鈕有一般外觀時顯示為線條,而不是按鈕之間的空格。 Appearance如果 屬性設定 ToolBarAppearance.Normal 為 ,則按鈕會引發並顯示三維,而分隔符號會顯示為按鈕之間的間距。

如果 屬性設定 ToolBarButtonStyle.DropDown 為 , Style 您可以將 指派 ContextMenu 給按鈕。 按一下按鈕時,就會顯示指派的功能表。

若要建立控制項集合 ToolBarButton 以顯示在 上 ToolBar ,請使用 Add 屬性的 Buttons 方法個別新增按鈕。 或者,您也可以使用 AddRange 方法來新增數個工具列按鈕。

建構函式

ToolBarButton()

初始化 ToolBarButton 類別的新執行個體。

ToolBarButton(String)

初始化 ToolBarButton 類別的新執行個體,並顯示按鈕上指派的文字。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
DropDownMenu

取得或設定要顯示於下拉式工具列按鈕的功能表。

Enabled

取得或設定值,指出是否啟用按鈕。

Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
ImageIndex

取得或設定指派給按鈕的影像的索引值。

ImageKey

取得或設定指派給按鈕的影像名稱。

Name

按鈕名稱。

Parent

取得被指派工具列按鈕的工具列控制項。

PartialPush

取得或設定值,指出是否部分壓下切換式工具列按鈕。

Pushed

取得或設定值,指出切換式工具列按鈕目前是否處於按下狀態。

Rectangle

取得工具列按鈕的週框 (Bounding Rectangle)。

Site

取得或設定 ComponentISite

(繼承來源 Component)
Style

取得或設定工具列按鈕的樣式。

Tag

取得或設定包含有關工具列按鈕的資料的物件。

Text

取得或設定顯示於工具列按鈕上的文字。

ToolTipText

取得或設定顯示為按鈕工具提示的文字。

Visible

取得或設定值,指出工具列按鈕是否為可見。

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 ToolBarButton 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToString()

傳回代表 ToolBarButton 控制項的字串。

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

適用於

另請參閱