|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
You use the toolbar control. In fact, probably the easiest way to add a toolbar to a program is to design that program with the Visual Basic Application Wizard. Well take a look at what the Application Wizard has to offer us, and then add a toolbar to a program ourselves. When you use the Application Wizard to create a program, that program gets a toolbar automatically. You can arrange and configure the toolbar with the Application Wizard Customize Toolbar dialog box, shown in Figure 15.4, which appears when you create a program with the Application Wizard.
The Application Wizard takes care of all the details for us. When you run the program it generates, you see a fully functional toolbar in that program, as shown in Figure 15.5.
However, most programmers will want to add their own toolbars to their programs, and you create a toolbar by adding a toolbar control to a form. Heres how that works:
This adds the Toolbar Control tool to the Visual Basic toolbox, as shown in Figure 15.1. To place a toolbar in your form, just double-click the Toolbar Control tool. Now youve got a new toolbarbut how do you align it at the top of the window and add buttons to it? See the next couple of topics in this chapter. Aligning Toolbars In A FormNow that youve added a toolbar to your form, where does it go? By default, it aligns itself with the top of the client area of the form. You can set the alignment of the toolbar with its Align property, which can take these values:
Adding Buttons To A ToolbarYouve got your new toolbar in the form you want and aligned it correctly. How about adding some buttons? You add buttons to a toolbar control at design time by right-clicking the control and clicking the Properties item in the menu that appears. When the toolbars property pages open, click the Buttons tab, as shown in Figure 15.6.
You insert new buttons by clicking the Insert Button button (and remove them with the Remove Button button). When you add a new button to a toolbar, you can associate a picture or caption with it. For example, to give a button a caption, just fill in the Caption box in Figure 15.6. Each button gets a new Index value, which will be passed to the Click event handler. You can also give each button a Key value, which is a string that you can use to identify the button. When youre done, click on the OK button to close the toolbars property pages. Now that youve installed buttons in your toolbar, how do you handle button clicks? Take a look at the next topic. Handling Toolbar Buttons ClicksNow that youve set up your toolbar with the buttons you want, how can you make those buttons active? You do that with the toolbar controls ButtonClick event: Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button) End Sub The button the user clicked is passed to us in this event handler procedure, and we can determine which button was clicked by checking either the buttons Index or Key properties. For example, we can indicate to users which button they clicked with a message box and the Index property this way:
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
MsgBox "You clicked button " & Button.Index
End Sub
All buttons in a toolbar control have an Index value by default (this value is 1-based), so this code is ready to go. When the user clicks a button, we report which button the user has clicked, as shown in Figure 15.7.
Besides using the Index property, you can also give each buttons Key property a text string (you do that at design time in the toolbar controls property pages). Then you use a Select Case statement to determine which button was clicked, like this:
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
Select Case Button.Key
Case "OpenFile"
OpenFile
Case "SaveFile"
SaveFile
Case "CloseFile"
CloseFile
End Select
End Sub
The complete code for the preceding code where we use the Index property appears in the toolbars folder on this books accompanying CD-ROM. Connecting Toolbar Buttons To Menu ItemsYou often use buttons in a toolbar as shortcuts for menu items. How do you connect a toolbar button to a menu item? You just call the menu items Click event handler when the button is clicked. For example, if you have three items in the File menu, Open, Save, and Close, that you want to connect to toolbar buttons, you can set those buttons Key properties to, say, OpenFile, SaveFile, and CloseFile, testing for those button clicks this way:
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
Select Case Button.Key
Case "OpenFile"
...
Case "SaveFile"
...
Case "CloseFile"
...
End Select
End Sub
If one of those buttons were clicked, you simply call the associated menu items Click event handler function directly:
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
Select Case Button.Key
Case "OpenFile"
mnuFileOpen_Click
Case "SaveFile"
mnuFileSave_Click
Case "CloseFile"
mnuFileClose_Click
End Select
End Sub
And thats all it takes. Now weve connected toolbar buttons to menu items. Adding Separators To A ToolbarThe Aesthetic Design Department is calling again. Cant you group the buttons in your toolbar into logical groups as you do with items in a menu? You can, and just in the same wayby using separators. In menus, separators appear as solid lines, but in toolbars, separators just appear as blank spaces, setting groups of buttons apart.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |