Export (0) Print
Expand All

ToolBarButtonClickEventArgs.Button Property

Gets or sets the ToolBarButton that was clicked.

[Visual Basic]
Public Property Button As ToolBarButton
[C#]
public ToolBarButton Button {get; set;}
[C++]
public: __property ToolBarButton* get_Button();
public: __property void set_Button(ToolBarButton*);
[JScript]
public function get Button() : ToolBarButton;
public function set Button(ToolBarButton);

Property Value

The ToolBarButton that was clicked.

Remarks

The Button property is initially set equal to the button parameter of the ToolBarButtonClickEventArgs constructor.

Example

[Visual Basic, C#, C++] The following example instantiates a ToolBar and three ToolBarButton controls. The toolbar buttons are assigned to the button collection, the collection is assigned to the toolbar, and the toolbar is added to the form. On the ButtonClick event of the toolbar, the Button property of the ToolBarButtonClickEventArgs is evaluated, and the appropriate dialog opened. This code assumes that a Form, an OpenFileDialog, a SaveFileDialog, and a PrintDialog have been instantiated.

[Visual Basic] 
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    

Protected Sub toolBar1_ButtonClick(sender As Object, _
    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


[C#] 
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);
 }
 
 protected 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; 
    }
 }


[C++] 
public:
    void InitializeMyToolBar() {
        // Create and initialize the ToolBar and ToolBarButton controls.
        toolBar1 = new ToolBar();
        ToolBarButton __gc *toolBarButton1 = new ToolBarButton();
        ToolBarButton __gc *toolBarButton2 = new ToolBarButton();
        ToolBarButton __gc *toolBarButton3 = new ToolBarButton();

        // Set the Text properties of the ToolBarButton controls.
        toolBarButton1->Text = S"Open";
        toolBarButton2->Text = S"Save";
        toolBarButton3->Text = S"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, &Form1::toolBar1_ButtonClick);

        // Add the ToolBar to the Form.
        Controls->Add(toolBar1);
    };

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ToolBarButtonClickEventArgs Class | ToolBarButtonClickEventArgs Members | System.Windows.Forms Namespace | ToolBarButton

Show:
© 2014 Microsoft