ToolBar.ToolBarButtonCollection.Contains(ToolBarButton) Method

Definition

Determines if the specified toolbar button is a member of the collection.

public:
 bool Contains(System::Windows::Forms::ToolBarButton ^ button);
public bool Contains (System.Windows.Forms.ToolBarButton button);
member this.Contains : System.Windows.Forms.ToolBarButton -> bool
Public Function Contains (button As ToolBarButton) As Boolean

Parameters

button
ToolBarButton

The ToolBarButton to locate in the collection.

Returns

true if the ToolBarButton is a member of the collection; otherwise, false.

Examples

The following code example removes an existing ToolBarButton from a ToolBar control if it exists and adds and inserts four new ToolBarButton objects to the ToolBar. This example requires that you have a Form with a ToolBar control on it.

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

Remarks

This method enables you to determine whether a ToolBarButton is member of the collection before attempting to perform operations on the ToolBarButton. You can use this method to confirm that a ToolBarButton has been added to or is still a member of the collection.

Applies to