.Net Controls: ToolBar

 

Overview

 

Introduction

A toolbar is a Windows control that allows the user the perform some actions on a form by clicking a button instead of using a menu. What a toolbar provides is a convenient group of buttons that simplifies the user's job by bringing the most accessible actions as buttons so that, instead of performing various steps to access a menu, a button on a toolbar can bring such common actions closer to the user.

Toolbars usually display under the main menu. They can be equipped with buttons but sometimes their buttons or some of their buttons have a caption. Toolbars can also be equipped with other types of controls.

To create a toolbar, on the Toolbox, click the Toolbox button and the form. By default, a toolbar is positioned on the top section of the form.

Like a form, a toolbar is only a container and does not provide much role by itself. To make a toolbar efficient, you should equip it with the necessary controls. To add controls to a toolbar, on the Properties window, click the ellipsis button of the Buttons field. This would open the ToolBarButton Collection Editor.

To add a control to a toolbar, in the ToolBarButton Collection Editor, click the Add button. The most common control used on a toolbar is a button. The types of buttons used are set using the Style property. A regular button has the PushButton style. The new button would appear empty.

A button on a toolbar is a rectangular object with a picture on top. The picture on the button should suggest what the button is used for but this is not always possible. Providing a picture is more important. The easiest way to provide pictures for your toolbar is by creating an image list and associating it with the toolbar. If a toolbar is given an image list, whenever you add a button, use the ImageIndex property to specify the image that would display on the button. You can keep adding buttons in this fashion.

A separator is a line that separates buttons (or controls) as groups of objects on a toolbar. To create a separator, after clicking Add, select the item and set its Style to Separator.

As a toolbar can be equipped with various types of controls, there are various types of buttons you can place on a toolbar. The types of buttons can be configured using the Style property on the Object Inspector.

  1. To create a new popup menu, on the Toolbox, click the ContextMenu button and click the form
  2. On the form, click ContextMenu and click Type Here
  3. Type &Staff and press Enter
  4. Type &Customer and press Enter
  5. Type &Book and press Enter
  6. Type &Author and press Enter
  7. Under the form, click contextMenu2 to select it. In the Properties window, click Name, type mnuNewContext and press Enter
  8. To create an image list, on the Toolbox, click the ImageList button and click the form
  9. While the new imageList1 control is still selected, in the Properties window, click the ellipsis button of the Images field
  10. In the Image Collection Editor, click the Add button
  11. Using the Open dialog box, locate the Drive:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\Bitmaps\Tbr_W95 folder. Click BACK and click Open
  12. From the same folder and in the same way, add the following bitmaps MCR, UNGROUP, and UP1LVL
  13. Click OK to close the Image Collection Editor
  14. To create a new toolbar, on the Toolbox, click the ToolBar button and click on the form
  15. On the Properties window,
    click the ImageList field and, from its combo box, select imageList1
    set the Appearance to Flat
  16. To add the controls to the toolbox, in the Properties window, click the ellipsis button of the Buttons field
  17. In the ToolBarButton Collection Editor, click the Add button
  18. In the Properties section, click the ImageIndex field and, from its combo box, select 0
  19. Click the Add button again and set its ImageIndex to 1
  20. Click the Add button again and, while the new item is still selected, click the arrow of the Style field and select Separator
  21. Click Add again. While the new item is still selected, in the Properties section
    click the arrow of the Style field and select DropDownButton
    Set its ImageIndex to 3
    click the arrow of the DropDownMenu and select mnuNewContext
  22. Click Add again and set the ImageIndex of this last button to 2
  23. Click OK to close the ToolBarButton Collection Editor
  24. To create a new popup menu, on the Toolbox, double-click ContextMenu
  25. Change its Name to mnuViewContext
  26. On the form, click Context Menu. Click Type Here. Type &Toolbar and press Enter. Type &Status Bar and press Enter
  27. Set the Checked property of each to True
  28. Click in the body of the form to select it
  29. On the main menu of the form, click View and double-click Toolbar
  30. Implement the event as follows:
     
    System::Void menuItem15_Click(System::Object *  sender, System::EventArgs *  e)
    	 {
    		 toolBar1->Visible = !toolBar1->Visible;
    		 menuItem15->Checked = !menuItem15->Checked;
    	 }
  31. On the form, double-click the m
  32. Test the application
     
    Using a drop down menu on a toolbar button
  33. Close it and return to MSVC

 

Toolbar Programming

The controls on a toolbar are controls by their owner, the toolbar. In order to write code for a control, you must first locate it. The buttons are located in a collection called Buttons. Each button can be located by calling the IndexOf() methods. Once this gives you access to a button, you can write its code as you see fit.

  1. On the form, double-click the toolbar to access its Click event
  2. Implement the event as follows:
     
    System::Void toolBar1_ButtonClick(System::Object *  sender,
                    System::Windows::Forms::ToolBarButtonClickEventArgs *  e)
    	 {
    		 int IndexOfButtonSelected = toolBar1->Buttons->IndexOf(e->Button);
    
    		 if( IndexOfButtonSelected == 4 )
    			 Close();
    	 }
  3. Execute the application
 

Home Copyright © 2004-2010 FunctionX, Inc.