Freigeben über


ToolBarButton-Klasse

Stellt eine Schaltfläche einer Windows-Symbolleiste dar. Obwohl ToolStripButton das ToolBarButton-Steuerelement früherer Versionen ersetzt und erweitert, können Sie ToolBarButton aus Gründen der Abwärtskompatibilität und für eine künftige Verwendung beibehalten.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Class ToolBarButton
    Inherits Component
'Usage
Dim instance As ToolBarButton
public class ToolBarButton : Component
public ref class ToolBarButton : public Component
public class ToolBarButton extends Component
public class ToolBarButton extends Component

Hinweise

ToolBarButton-Steuerelemente sind ToolBar-Steuerelementen untergeordnet. Nach dem Erstellen der Symbolleisten-Schaltfläche müssen die allgemeinen Eigenschaften Text und ImageIndex festgelegt werden. Legen Sie die Text-Eigenschaft der Schaltfläche fest, um Text unter dem oder rechts vom Bild anzuzeigen. Weisen Sie den Schaltflächen Bilder zu, indem Sie eine ImageList erstellen, die Sie der ImageList-Eigenschaft der Symbolleiste zuweisen. Weisen Sie anschließend den Indexwert des Bildes der ImageIndex-Eigenschaft der Schaltfläche zu.

Sie können die Darstellung der Symbolleisten-Schaltflächen ändern, indem Sie die Appearance-Eigenschaft des übergeordneten Symbolleisten-Steuerelements festlegen. Bei der ToolBarAppearance.Flat-Darstellung werden die Schaltflächen flach dargestellt. Sobald sich der Mauszeiger über den Schaltflächen befindet, werden diese dreidimensional dargestellt. Schaltflächen-Trennlinien werden bei einer flachen Schaltflächendarstellung als Linien und nicht als Zwischenräume zwischen den Schaltflächen angezeigt. Wenn die Appearance-Eigenschaft auf ToolBarAppearance.Normal festgelegt ist, werden die Schaltflächen angehoben und dreidimensional dargestellt, und die Trennung erfolgt durch Zwischenräume zwischen den Schaltflächen.

Sie können einer Schaltfläche ein ContextMenu zuweisen, indem Sie die Style-Eigenschaft auf ToolBarButtonStyle.DropDown festlegen. Beim Klicken auf die Schaltfläche wird das zugeordnete Menü angezeigt.

Zum Erstellen einer Auflistung von ToolBarButton-Steuerelementen, die auf einer ToolBar angezeigt werden sollen, fügen Sie die Schaltflächen einzeln mithilfe der Add-Methode der Buttons-Eigenschaft hinzu. Mit der AddRange-Methode können Sie auch mehrere Symbolleisten-Schaltflächen hinzufügen.

Beispiel

Im folgenden Codebeispiel werden eine ToolBar und drei ToolBarButton-Steuerelemente erstellt. Jede Symbolleisten-Schaltfläche wird der Auflistung der Schaltflächen und diese Auflistung der Symbolleiste zugewiesen. Die Symbolleiste wird dann dem Formular hinzugefügt. Beim Eintreten des ButtonClick-Ereignisses der Symbolleiste wird die Button-Eigenschaft von ToolBarButtonClickEventArgs ausgewertet, und das entsprechende Dialogfeld wird geöffnet. Für dieses Beispiel ist es erforderlich, dass ein Form, ein OpenFileDialog, ein SaveFileDialog und ein PrintDialog erstellt wurden.

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
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:
   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.set_Text("Open");
    toolBarButton2.set_Text("Save");
    toolBarButton3.set_Text("Print");

    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.get_Buttons().Add(toolBarButton1);
    toolBar1.get_Buttons().Add(toolBarButton2);
    toolBar1.get_Buttons().Add(toolBarButton3);

    // Add the event-handler delegate.
    toolBar1.add_ButtonClick(new ToolBarButtonClickEventHandler(
                                 this.toolBar1_ButtonClick));

    // Add the ToolBar to the Form.
    get_Controls().Add(toolBar1);
} //InitializeMyToolBar

protected void toolBar1_ButtonClick(Object sender,
                                    ToolBarButtonClickEventArgs e)
{
    // Evaluate the Button property to determine which button was clicked.
    switch (toolBar1.get_Buttons().IndexOf(e.get_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;
    } 
} //toolBar1_ButtonClick

Vererbungshierarchie

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Windows.Forms.ToolBarButton

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ToolBarButton-Member
System.Windows.Forms-Namespace
ToolBar-Klasse
MenuItem-Klasse
ImageList-Klasse
ToolTip