ToolBar Konstruktor
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menginisialisasi instans baru kelas ToolBar.
public:
ToolBar();
public ToolBar ();
Public Sub New ()
Contoh
Contoh kode berikut membuat ToolBar dan tiga ToolBarButton kontrol. Tombol toolbar ditetapkan ke koleksi tombol, koleksi ditetapkan ke toolbar, dan toolbar ditambahkan ke formulir. ButtonClick Pada kejadian toolbar, Button properti ToolBarButtonClickEventArgs dievaluasi, dan kotak dialog yang sesuai dibuka. Kode ini mengharuskan Form, OpenFileDialog, , SaveFileDialogdan PrintDialog telah dibuat.
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.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 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
Keterangan
Kontrol toolbar yang baru dibuat kosong; tambahkan ToolBarButton kontrol dengan mengatur Buttons properti .