Nasıl yapılır: Araç Çubuğu Düğmeleri için Menü Olaylarını Tetikleme

Not

Denetim, denetimin yerini alan ve denetime işlevsellik ekler; ancak, siz seçerseniz denetim hem geriye dönük uyumluluk hem de gelecekteki ToolStripToolBar kullanım için ToolBar korunur.

Windows Form'uz araç çubuğu düğmeleriyle bir denetime sahipse, kullanıcının hangi ToolBar düğmeye tıklar olduğunu bilmek istersiniz.

Denetimin ButtonClickToolBar olayında sınıfının özelliğini ButtonToolBarButtonClickEventArgs değerlendirin. Aşağıdaki örnekte, hangi düğmeye tıklandı olduğunu gösteren bir ileti kutusu gösterilir. Ayrıntılar için bkz. MessageBox.

Aşağıdaki örnekte, Windows ToolBar Form'a bir denetimin ek Windows vardır.

Araç çubuğunda Tıklama olayı işlemek için

  1. Bir yordamda, denetime araç çubuğu düğmeleri ToolBar ekleyin.

    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->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. Denetimin olayı için bir ToolBar olay ButtonClick işleyicisi ekleyin. Tıklandı araç çubuğu düğmesini belirlemek ToolBarButtonClickEventArgs için bir durum değiştirme deyimini ve sınıfını kullanın. Buna bağlı olarak, uygun bir ileti kutusu göster.

    Not

    Bu örnekte yalnızca yer tutucu olarak bir ileti kutusu kullanılıyor. Araç çubuğu düğmelerine tıklarsanız yürütülecek başka kodlar da ekleyebilirsiniz.

    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->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;  
          }  
       }  
    

Ayrıca bkz.