ToolBar.Buttons 属性

定义

获取分配给工具栏控件的 ToolBarButton 控件集合。Gets the collection of ToolBarButton controls assigned to the toolbar control.

public:
 property System::Windows::Forms::ToolBar::ToolBarButtonCollection ^ Buttons { System::Windows::Forms::ToolBar::ToolBarButtonCollection ^ get(); };
public System.Windows.Forms.ToolBar.ToolBarButtonCollection Buttons { get; }
member this.Buttons : System.Windows.Forms.ToolBar.ToolBarButtonCollection
Public ReadOnly Property Buttons As ToolBar.ToolBarButtonCollection

属性值

ToolBar.ToolBarButtonCollection

一个包含 ToolBar.ToolBarButtonCollection 控件集合的 ToolBarButtonA ToolBar.ToolBarButtonCollection that contains a collection of ToolBarButton controls.

示例

下面的代码示例创建并初始化一个 ToolBar 和三个 ToolBarButton 控件。The following code example creates and initializes a ToolBar and three ToolBarButton controls. 工具栏按钮将分配给工具栏,工具栏将添加到窗体中。The toolbar buttons are assigned to the toolbar and the toolbar is added to the form. 此代码需要已 Form 创建的。This code requires that a Form has already been created.

public:
   void InitializeMyToolBar()
   {
      // Create and initialize the ToolBarButton controls and ToolBar.
      ToolBar^ 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 ToolBar to the Form.
      Controls->Add( toolBar1 );
   }
public void InitializeMyToolBar()
 {
    // Create and initialize the ToolBarButton controls and ToolBar.
    ToolBar 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 ToolBar to the Form.
    Controls.Add(toolBar1);
 }

Public Sub InitializeMyToolBar()
    ' Create and initialize the ToolBarButton controls and ToolBar.
    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 ToolBar to the Form.
    Controls.Add(toolBar1)
End Sub

注解

Buttons属性是一个从零开始的索引集合,用于保存 ToolBarButton 分配给工具栏的所有控件。The Buttons property is a zero-based indexed collection used to hold all the ToolBarButton controls assigned to the toolbar. 由于属性是只读的,因此不能直接向其分配工具栏按钮的集合。Because the property is read-only, it can not be assigned a collection of toolbar buttons directly. 可以通过使用从类继承的方法来添加或移除工具栏按钮 ToolBar.ToolBarButtonCollectionToolbar buttons can be added or removed by using the methods inherited from the ToolBar.ToolBarButtonCollection class. 使用 Add 方法可添加各个按钮和 Remove 方法以删除按钮。Use the Add method to add individual buttons and the Remove method to delete a button. 调用 Clear 方法可从集合中移除所有按钮。Call the Clear method to remove all the buttons from the collection.

适用于

另请参阅