ToolBarButtonClickEventArgs 类

ButtonClick 事件提供数据。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Class ToolBarButtonClickEventArgs
    Inherits EventArgs
用法
Dim instance As ToolBarButtonClickEventArgs
public class ToolBarButtonClickEventArgs : EventArgs
public ref class ToolBarButtonClickEventArgs : public EventArgs
public class ToolBarButtonClickEventArgs extends EventArgs
public class ToolBarButtonClickEventArgs extends EventArgs

备注

当用户单击 ToolBar 控件上的按钮时,就会发生该事件。Button 属性包含 ToolBarButton,并包含被单击的按钮的信息。

示例

下面的示例实例化一个 ToolBar 和三个 ToolBarButton 控件。这些工具栏按钮将被分配到按钮集合,该集合又被分配到工具栏,而工具栏将被添加到窗体上。当工具栏的 ButtonClick 事件发生时,将计算 ToolBarButtonClickEventArgsButton 属性,并打开适当的对话框。此代码假定已经实例化一个 Form、一个 OpenFileDialog、一个 SaveFileDialog 和一个 PrintDialog

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

继承层次结构

System.Object
   System.EventArgs
    System.Windows.Forms.ToolBarButtonClickEventArgs

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ToolBarButtonClickEventArgs 成员
System.Windows.Forms 命名空间
ToolBar 类
ToolBarButton 类