ToolBar.ToolBarButtonCollection.IndexOf(ToolBarButton) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Haalt de index van de opgegeven werkbalkknop in de verzameling op.
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
Parameters
- button
- ToolBarButton
De ToolBarButton te vinden in de verzameling.
Retouren
De op nul gebaseerde index van het item dat in de verzameling is gevonden; anders, -1.
Voorbeelden
In het volgende codevoorbeeld worden een ToolBar en drie ToolBarButton besturingselementen gemaakt. De werkbalkknoppen worden toegewezen aan de knopverzameling, de verzameling wordt toegewezen aan de werkbalk en de werkbalk wordt toegevoegd aan het formulier. In de ButtonClick gebeurtenis van de werkbalk wordt de Button eigenschap van de ToolBarButtonClickEventArgs werkbalk geƫvalueerd en het juiste dialoogvenster geopend. Voor deze code is vereist dat een Form, een OpenFileDialog, een SaveFileDialogen een PrintDialog allemaal zijn gemaakt.
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
Opmerkingen
Met deze methode hebt u eenvoudig toegang tot de indexwaarde van de ToolBarButton in de verzameling. Met de indexwaarde kunt u eenvoudig bepalen welke ToolBarButton erop is geklikt ToolBar. De ToolBarButton geklikte waarde kan worden bepaald door de IndexOf waarde van de ToolBarButtonClickEventArgs.Button eigenschap te evalueren.