先在工具箱中双击ToolBal控件
然后在属性窗口中找到Buttons,点击旁边的按钮
弹出对话框后,点击添加即可添加在工具栏中的按钮
再修改各个属性即可若要在按钮上加上图片,首先要添加一个ImageList控件,在选中Image属性添加图片
然后选择工具条属性中的Imagelis属性,在下拉菜单中选中刚刚创建的ImageList控件名即可
然后在属性窗口中找到Buttons,点击旁边的按钮
弹出对话框后,点击添加即可添加在工具栏中的按钮
再修改各个属性即可若要在按钮上加上图片,首先要添加一个ImageList控件,在选中Image属性添加图片
然后选择工具条属性中的Imagelis属性,在下拉菜单中选中刚刚创建的ImageList控件名即可
可是那么多的按钮和那么多的图片,怎么一一对应?
有你刚才在ImageList控件中添加的图片,选择即可
[Visual Basic, C#] 以下示例实例化一个 ToolBar 和三个 ToolBarButton 控件。这些工具栏按钮将被分配到按钮集合,该集合又被分配到工具栏,而工具栏将被添加到窗体上。当工具栏的 ButtonClick 事件发生时,将计算 ToolBarButtonClickEventArgs 的 Button 属性,并打开适当的对话框。此代码假定已经实例化一个 Form、一个 OpenFileDialog、一个 SaveFileDialog 和一个 PrintDialog。[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;
}
}
在同一过程中,向该 ImageList 组件分配一个图像。
在同一过程中,向该 ToolBar 控件分配 ImageList 控件,并分配单个工具栏按钮的 ImageIndex 属性。
按照上述步骤,您应该已经编写出类似于下面显示的代码。 ' Visual Basic
Public Sub InitializeMyToolBar()
' Instantiate an ImageList component and a ToolBar control.
Dim ToolBar1 as New ToolBar
Dim ImageList1 as New ImageList
' Assign an image to the ImageList component.
' The image used below is for demonstration purposes only.
' Replace it with an image of your own choosing.
Dim myImage As System.Drawing.Image = Image.FromFile("C:\winnt\Sample.ico")
ImageList1.Images.Add(myImage)
' Create a ToolBarButton.
Dim ToolBarButton1 As New ToolBarButton()
' Add the ToolBarButton to the ToolBar.
ToolBar1.Buttons.Add(toolBarButton1)
' Assign an ImageList to the ToolBar.
ToolBar1.ImageList = ImageList1
' Assign the ImageIndex property of the ToolBarButton.
ToolBarButton1.ImageIndex = 0
End Sub// C#
public void InitializeMyToolBar()
{
// Instantiate an ImageList component and a ToolBar control.
ToolBar toolBar1 = new ToolBar();
ImageList imageList1 = new ImageList();
// Assign an image to the ImageList component.
// The image used below is for demonstration purposes only.
// Replace it with an image of your own choosing.
Image myImage = Image.FromFile("C:\\winnt\\Sample.ico");
ImageList1.Images.Add(myImage);
// Create a ToolBarButton.
ToolBarButton toolBarButton1 = new ToolBarButton();
// Add the ToolBarButton to the ToolBar.
toolBar1.Buttons.Add(toolBarButton1);
// Assign an ImageList to the ToolBar.
toolBar1.ImageList = imageList1;
// Assign ImageIndex property of the ToolBarButton.
toolBarButton1.ImageIndex = 0;
}