ToolBar.ToolBarButtonCollection.Add 方法

定義

將新的工具列按鈕加入至工具列按鈕集合的結尾。

多載

Add(String)

使用指定的 Text 屬性值將新的工具按鈕加入至工具列按鈕集合的結尾。

Add(ToolBarButton)

將指定的工具列按鈕加入工具列按鈕集合的結尾。

Add(String)

使用指定的 Text 屬性值將新的工具按鈕加入至工具列按鈕集合的結尾。

public:
 int Add(System::String ^ text);
public int Add (string text);
member this.Add : string -> int
Public Function Add (text As String) As Integer

參數

text
String

要顯示在新 ToolBarButton 上的文字。

傳回

Int32

加入集合之 ToolBarButton 以零為起始的索引值。

範例

下列程式碼範例會從 ToolBar 控制項中移除現有的 ToolBarButton ,如果存在,並將四個新 ToolBarButton 物件插入 至 ToolBar 。 此範例會要求您具有 Form ToolBar 控制項的 。

void AddToolbarButtons( ToolBar^ toolBar )
{
   if (  !toolBar->Buttons->IsReadOnly )
   {
      
      // If toolBarButton1 in in the collection, remove it.
      if ( toolBar->Buttons->Contains( toolBarButton1 ) )
      {
         toolBar->Buttons->Remove( toolBarButton1 );
      }
      
      // Create three toolbar buttons.
      ToolBarButton^ tbb1 = gcnew ToolBarButton( "tbb1" );
      ToolBarButton^ tbb2 = gcnew ToolBarButton( "tbb2" );
      ToolBarButton^ tbb3 = gcnew ToolBarButton( "tbb3" );
      
      // Add toolbar buttons to the toolbar.
      array<ToolBarButton^>^buttons = {tbb2,tbb3};
      toolBar->Buttons->AddRange( buttons );
      toolBar->Buttons->Add( "tbb4" );
      
      // Insert tbb1 into the first position in the collection.
      toolBar->Buttons->Insert( 0, tbb1 );
   }
}
private void AddToolbarButtons(ToolBar toolBar)
{
   if(!toolBar.Buttons.IsReadOnly)
   {
      // If toolBarButton1 in in the collection, remove it.
      if(toolBar.Buttons.Contains(toolBarButton1))
      {
         toolBar.Buttons.Remove(toolBarButton1);
      }
    
      // Create three toolbar buttons.
      ToolBarButton tbb1 = new ToolBarButton("tbb1");
      ToolBarButton tbb2 = new ToolBarButton("tbb2");
      ToolBarButton tbb3 = new ToolBarButton("tbb3");
      
      // Add toolbar buttons to the toolbar.		
      toolBar.Buttons.AddRange(new ToolBarButton[] {tbb2, tbb3});
      toolBar.Buttons.Add("tbb4");
    
      // Insert tbb1 into the first position in the collection.
      toolBar.Buttons.Insert(0, tbb1);
   }
}
Private Sub AddToolbarButtons(toolBar As ToolBar)
   If Not toolBar.Buttons.IsReadOnly Then
      ' If toolBarButton1 in in the collection, remove it.
      If toolBar.Buttons.Contains(toolBarButton1) Then
         toolBar.Buttons.Remove(toolBarButton1)
      End If

      ' Create three toolbar buttons.
      Dim tbb1 As New ToolBarButton("tbb1")
      Dim tbb2 As New ToolBarButton("tbb2")
      Dim tbb3 As New ToolBarButton("tbb3")

      ' Add toolbar buttons to the toolbar.		
      toolBar.Buttons.AddRange(New ToolBarButton() {tbb2, tbb3})
      toolBar.Buttons.Add("tbb4")

      ' Insert tbb1 into the first position in the collection.
      toolBar.Buttons.Insert(0, tbb1)
   End If
End Sub

備註

您也可以使用 AddRangeInsert 方法或其他版本的 Add 方法,將新 ToolBarButton 物件加入至集合。

若要移除您先前新增的 ToolBarButton ,請使用 RemoveRemoveAt Clear 方法。

另請參閱

適用於

Add(ToolBarButton)

將指定的工具列按鈕加入工具列按鈕集合的結尾。

public:
 int Add(System::Windows::Forms::ToolBarButton ^ button);
public int Add (System.Windows.Forms.ToolBarButton button);
member this.Add : System.Windows.Forms.ToolBarButton -> int
Public Function Add (button As ToolBarButton) As Integer

參數

button
ToolBarButton

ToolBarButton,加入至所有現有按鈕之後。

傳回

Int32

加入集合之 ToolBarButton 以零為起始的索引值。

範例

下列程式碼範例會將新的 ToolBarButton 控制項新增至現有的 ToolBar 按鈕。 工具列按鈕將會新增至集合結尾 ToolBar.Buttons

public:
   void AddMyButton()
   {
      ToolBarButton^ toolBarButton1 = gcnew ToolBarButton;
      toolBarButton1->Text = "Print";
      
      // Add the new toolbar button to the toolbar.
      toolBar1->Buttons->Add( toolBarButton1 );
   }
public void AddMyButton()
 {
    ToolBarButton toolBarButton1 = new ToolBarButton();
    toolBarButton1.Text = "Print";
 
    // Add the new toolbar button to the toolbar.
    toolBar1.Buttons.Add(toolBarButton1);
 }
Public Sub AddMyButton()
    Dim toolBarButton1 As New ToolBarButton()
    toolBarButton1.Text = "Print"
    
    ' Add the new toolbar button to the toolbar.
    toolBar1.Buttons.Add(toolBarButton1)
End Sub

備註

您也可以使用 AddRangeInsert 方法或其他版本的 Add 方法,將新 ToolBarButton 物件加入至集合。

若要移除您先前新增的 ToolBarButton ,請使用 RemoveRemoveAt Clear 方法。

另請參閱

適用於