ToolBar.ToolBarButtonCollection.IndexOf(ToolBarButton) 方法

定义

检索集合中指定工具栏按钮的索引。

public:
 int IndexOf(System::Windows::Forms::ToolBarButton ^ button);
public int IndexOf (System.Windows.Forms.ToolBarButton button);
member this.IndexOf : System.Windows.Forms.ToolBarButton -> int
Public Function IndexOf (button As ToolBarButton) As Integer

参数

button
ToolBarButton

要在集合中查找的 ToolBarButton

返回

集合中项的从零开始的索引(如果找到);否则为 -1。

示例

下面的代码示例创建 一个 和三ToolBarButtonToolBar控件。 工具栏按钮分配给按钮集合,集合分配给工具栏,工具栏添加到窗体。 在 ButtonClick 工具栏的 事件中 Button ,计算 的 ToolBarButtonClickEventArgs 属性,并打开相应的对话框。 此代码要求 Form已全部创建 、 OpenFileDialogSaveFileDialog、 和 PrintDialog

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 additional code here to open the file.
            break;
         case 1:
            saveFileDialog1->ShowDialog();
            // Insert additional code here to save the file.
            break;
         case 2:
            printDialog1->ShowDialog();
            // Insert additional code here 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.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(
       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 additional code here to open the file.
         break; 
      case 1:
         saveFileDialog1.ShowDialog();
         // Insert additional code here to save the file.
         break; 
      case 2:
         printDialog1.ShowDialog();
         // Insert additional code here to print the file.    
         break; 
    }
 }
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 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 additional code here to open the file.
        Case 1
            saveFileDialog1.ShowDialog()
            ' Insert additional code here to save the file.
        Case 2
            printDialog1.ShowDialog()
            ' Insert additional code here to print the file.    
    End Select
End Sub

注解

使用此方法可以轻松访问集合中 的 ToolBarButton 索引值。 索引值使你可以轻松地确定在 上单击了ToolBar哪个 ToolBarButtonToolBarButton可以通过计算 IndexOf 属性的值ToolBarButtonClickEventArgs.Button来确定单击的 。

适用于

另请参阅