Partager via


Comment : déclencher des événements de menu pour les boutons de barre d'outils

Notes

Le contrôle ToolStrip remplace le contrôle ToolBar et lui ajoute des fonctionnalités ; toutefois, le contrôle ToolBar est conservé pour la compatibilité descendante et l'utilisation future si tel est votre choix.

Si votre Windows Form contient un contrôle ToolBar avec des boutons de barre d'outils, vous voudrez savoir sur quel bouton clique l'utilisateur.

Vous pouvez évaluer la propriété Button de la classe ToolBarButtonClickEventArgs sur l'événement ButtonClick du contrôle ToolBar. Dans l'exemple ci-dessous, une boîte de message indiquant sur quel bouton l'utilisateur a cliqué s'affiche. Pour plus d'informations, consultez MessageBox, classe.

L'exemple ci-dessous suppose qu'un contrôle ToolBar a été ajouté à un Windows Form.

Pour prendre en charge l'événement Click dans une barre d'outils

  1. Dans une procédure, ajoutez des boutons de barre d'outils au contrôle ToolBar.

    Public Sub ToolBarConfig()
    ' Instantiate the toolbar buttons, set their Text properties
    ' and add them to the ToolBar control.
       ToolBar1.Buttons.Add(New ToolBarButton("One"))
       ToolBar1.Buttons.Add(New ToolBarButton("Two"))
       ToolBar1.Buttons.Add(New ToolBarButton("Three"))
    ' Add the event handler delegate.
       AddHandler ToolBar1.ButtonClick, AddressOf Me.ToolBar1_ButtonClick
    End Sub
    
    public void ToolBarConfig() 
    {
       toolBar1.Buttons.Add(new ToolBarButton("One"));
       toolBar1.Buttons.Add(new ToolBarButton("Two"));
       toolBar1.Buttons.Add(new ToolBarButton("Three"));
    
       toolBar1.ButtonClick += 
          new ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
    }
    
    public void ToolBarConfig() 
    {
       toolBar1.get_Buttons().Add(new ToolBarButton("One"));
       toolBar1.get_Buttons().Add(new ToolBarButton("Two"));
       toolBar1.get_Buttons().Add(new ToolBarButton("Three"));
    
       toolBar1.add_ButtonClick(new ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick));
    }
    
    public:
       void ToolBarConfig()
       {
          toolBar1->Buttons->Add(gcnew ToolBarButton("One"));
          toolBar1->Buttons->Add(gcnew ToolBarButton("Two"));
          toolBar1->Buttons->Add(gcnew ToolBarButton("Three"));
    
          toolBar1->ButtonClick += 
             gcnew ToolBarButtonClickEventHandler(this,
             &Form1::toolBar1_ButtonClick);
       }
    
  2. Ajoutez un gestionnaire d'événements pour l'événement ButtonClick du contrôle ToolBar. Utilisez une instruction switch Case et la classe ToolBarButtonClickEventArgs pour identifier le bouton de barre d'outils sur lequel l'utilisateur a cliqué. En fonction du bouton identifié, affichez une boîte de message appropriée.

    Notes

    Une boîte de message est utilisée uniquement comme espace réservé dans cet exemple. Vous pouvez ajouter du code supplémentaire à exécuter lorsque l'utilisateur clique sur les boutons de barre d'outils.

    Protected Sub ToolBar1_ButtonClick(ByVal sender As Object, _
    ByVal e As ToolBarButtonClickEventArgs)
    ' Evaluate the Button property of the ToolBarButtonClickEventArgs
    ' to determine which button was clicked.
       Select Case ToolBar1.Buttons.IndexOf(e.Button)
         Case 0
           MessageBox.Show("First toolbar button clicked")
         Case 1
           MessageBox.Show("Second toolbar button clicked")
         Case 2
           MessageBox.Show("Third toolbar button clicked")
       End Select
    End Sub
    
    protected void toolBar1_ButtonClick(object sender,
    ToolBarButtonClickEventArgs e)
    {
       // Evaluate the Button property of the ToolBarButtonClickEventArgs
       // to determine which button was clicked.
       switch (toolBar1.Buttons.IndexOf(e.Button))
       {
          case 0 :
             MessageBox.Show("First toolbar button clicked");
             break;
          case 1 :
             MessageBox.Show("Second toolbar button clicked");
             break;
          case 2 :
             MessageBox.Show("Third toolbar button clicked");
             break;
       }
    }
    
    protected void toolBar1_ButtonClick(System.Object sender, ToolBarButtonClickEventArgs e) 
    {
       // Evaluate the Button property of the ToolBarButtonClickEventArgs
       // to determine which button was clicked.
       switch(toolBar1.get_Buttons().IndexOf(e.get_Button())) 
       {
          case 0 : 
             MessageBox.Show("First toolbar button clicked");
             break;
          case 1 : 
             MessageBox.Show("Second toolbar button clicked");
             break;
          case 2 : 
             MessageBox.Show("Third toolbar button clicked");
             break;
       }
    }
    
    protected:
       void toolBar1_ButtonClick(System::Object ^ sender,
          ToolBarButtonClickEventArgs ^ e)
       {
         // Evaluate the Button property of the ToolBarButtonClickEventArgs
         // to determine which button was clicked.
          switch (toolBar1->Buttons->IndexOf(e->Button))
          {
             case 0 :
                MessageBox::Show("First toolbar button clicked");
                break;
             case 1 :
                MessageBox::Show("Second toolbar button clicked");
                break;
             case 2 :
                MessageBox::Show("Third toolbar button clicked");
                break;
          }
       }
    

Voir aussi

Tâches

Comment : ajouter des boutons à un contrôle ToolBar

Comment : définir une icône pour un bouton de barre d'outils

Référence

ToolBar

Autres ressources

ToolBar, contrôle (Windows Forms)