ToolBar Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
'Declaration <ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class ToolBar Inherits Control 'Usage Dim instance As ToolBar
/** @attribute ComVisibleAttribute(true) */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ public class ToolBar extends Control
ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) public class ToolBar extends Control
ToolBar controls are used to display ToolBarButton controls that can appear as a standard button, a toggle-style button, or a drop-down style button. You can assign images to the buttons by creating an ImageList, assigning it to the ImageList property of the toolbar, and assigning the image index value to the ImageIndex property each ToolBarButton. You can then assign text to be displayed underneath or to the right of the image by setting the Text property of the ToolBarButton.
Set the Appearance property of the toolbar to Flat to give the toolbar and its buttons a flat appearance. As the mouse pointer moves over the buttons, their appearance changes to three-dimensional. Toolbar buttons can be divided into logical groups by using separators. A separator is a toolbar button with the Style property set to ToolBarButtonStyle.Separator. Button separators appear as lines rather than spaces between the buttons when the toolbar has a flat appearance. If the Appearance property is set to Normal, the toolbar buttons appear raised and three-dimensional.
If you specify a value for the ButtonSize property, all buttons in the tool bar are restricted to the specified size. Otherwise, the buttons adjust their size depending on their content, and the ButtonSize property returns the initial size of the largest button.
To create a collection of ToolBarButton controls to display on the ToolBar, add the buttons individually by using the Add or Insert methods of the Buttons property.
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: A form supports only one ToolBar, attempts to add an additional ToolBar throws a NotSupportedException. Adding a ToolBar to any control besides a form is not supported, such as to a Panel.
The following code example creates a ToolBar and three ToolBarButton controls. The toolbar buttons are assigned to the button collection, the collection is assigned to the toolbar, and the toolbar is added to the form. On the ButtonClick event of the toolbar, the Button property of the ToolBarButtonClickEventArgs is evaluated, and the appropriate dialog box opened. This code requires that a Form, an OpenFileDialog, a SaveFileDialog, and a PrintDialog have been created.
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.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.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ToolBar
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.