Tải bản đầy đủ (.pdf) (112 trang)

Visual Basic 6 Black Book phần 5 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.38 MB, 112 trang )

" VbShapeOval2; oval
" VbShapeCircle3; circle
" VbShapeRoundedRectangle4; rounded rectangle
" VbShapeRoundedSquare5; rounded square
One important use of shape controls is to group other controls together. (Note, however, that shape
controls cant act as true control containers in the way picture boxes or frames can. For example, you
cant group option buttons together with shapes.) In Figure 14.10, were using shape controls to group
the buttons visually into two groups.
Figure 14.10 Using the shape control to group other controls.
You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape
using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the
shapes drawing line, including using dots and dashes. For more on this control, see the other topics in
this chapter.



Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\446-449.html (3 of 3) [3/14/2001 1:48:23 AM]
Simpo PDF Merge and Split Unregistered Version -


To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Bookmark It
Search this book:

PreviousTable of ContentsNext



Drawing Rectangles
How do you draw rectangles with the shape control? You start by clicking the Shape
Control tool in the Visual Basic toolbox and drawing that control to match the size
and location you want your new figure to have. To draw a rectangle, you simply set
the controls Shape property to VbShapeRectangle (thats the default anyway).
Thats all you have to dothe shape control is very easy to work with. Using the shape
control, you can draw in both forms and picture boxes.
You can also set the width of the shapes drawing line with the BorderWidth
property and fill the shape using the FillColor and FillStyle properties. The
BorderStyle property lets you select the style of the shapes drawing line, including
using dots and dashes.
Drawing Squares
How do you draw squares with the shape control? You start by clicking the Shape
Control tool in the Visual Basic toolbox and drawing that control to match the size
and location you want your new figure to have. To draw a square, you simply set the
controls Shape property to VbShapeSquare.
Thats all you have to dothe shape control is simple. Using the shape control, you can
draw in both forms and picture boxes.
You can also set the width of the shapes drawing line with the BorderWidth
property and fill the shape using the FillColor and FillStyle properties. The
BorderStyle property lets you select the style of the shapes drawing line, including
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\449-452.html (1 of 3) [3/14/2001 1:48:31 AM]
Simpo PDF Merge and Split Unregistered Version -
Go!
using dots and dashes. For more on this control, see the other topics in this chapter.
Drawing Ovals
To draw ovals with the shape control, you start by clicking the Shape Control tool in
the Visual Basic toolbox and drawing that control to match the size and location you

want your new figure to have. To draw an oval, you simply set the controls Shape
property to VbShapeOval.
Thats all you have to dothe shape control is very easy. Using the shape control, you
can draw in both forms and picture boxes.
You can also set the width of the shapes drawing line with the BorderWidth
property and fill the shape using the FillColor and FillStyle properties. The
BorderStyle property lets you select the style of the shapes drawing line, including
using dots and dashes. For more on this control, see the other topics in this chapter.
Drawing Circles
To draw circles, you start by clicking the Shape Control tool in the Visual Basic
toolbox and drawing that control to match the size and location you want your new
figure to have. To draw a circle, you simply set the controls Shape property to
VbShapeCircle.
Thats all you have to dothe shape control is very easy to work with. Using the shape
control, you can draw in both forms and picture boxes.
You can also set the width of the shapes drawing line with the BorderWidth
property and fill the shape using the FillColor and FillStyle properties. The
BorderStyle property lets you select the style of the shapes drawing line, including
using dots and dashes. For more on this control, see the other topics in this chapter.
Drawing Rounded Rectangles
How do you draw rounded rectangles with the shape control? You start by clicking
the Shape Control tool in the Visual Basic toolbox and drawing that control to match
the size and location you want your new figure to have. To draw a rounded rectangle,
you simply set the controls Shape property to VbShapeRoundedRectangle.
Thats all you have to dothis control is very easy. Using the shape control, you can
draw in both forms and picture boxes.
You can also set the width of the shapes drawing line with the BorderWidth
property, and fill the shape using the FillColor and FillStyle properties. The
BorderStyle property lets you select the style of the shapes drawing line, including
using dots and dashes. For more on this control, see the other topics in this chapter.

Drawing Rounded Squares
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\449-452.html (2 of 3) [3/14/2001 1:48:31 AM]
Simpo PDF Merge and Split Unregistered Version -
To draw rounded squares with the shape control, you start by clicking the Shape
Control tool in the Visual Basic toolbox and drawing that control to match the size
and location you want your new figure to have. To draw a rounded square, you simply
set the controls Shape property to VbShapeRoundedSquare.
Thats all you have to do. The shape control is easy. Using the shape control, you can
draw in both forms and picture boxes.
You can also set the width of the shapes drawing line with the BorderWidth
property and fill the shape using the FillColor and FillStyle properties. The
BorderStyle property lets you select the style of the shapes drawing line, including
using dots and dashes. For more on this control, see the other topics in this chapter.
Setting Shape Borders: Drawing Width, Dashes, And Dots
The Aesthetic Design Department is on the line. Cant you do something about the
shapes in your program? Maybe make themdotted? You think, dotted?
Visual Basic can help here. Just set the shape controls BorderStyle property. Here
are the possible values for the BorderStyle property:
" vbTransparent0; transparent
" vbBSSolid1 (the default); solid (the border is centered on the edge of the shape)
" vbBSDash2; dash
" vbBSDot3; dot
" vbBSDashDot4; dash-dot
" vbBSDashDotDot5; dash-dot-dot
" vbBSInsideSolid6; inside solid (the outer edge of the border is the outer edge of
the shape)
Using this property, you can adjust the border of your shape control as you want it.
Heres another way to customize a shape control: you can set the shape controls
border width (in other words, the drawing line width) using the shape controls

BorderWidth property. Just set that property to the new value you want for the
border thickness (the default value is 1).




Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\449-452.html (3 of 3) [3/14/2001 1:48:31 AM]
Simpo PDF Merge and Split Unregistered Version -
Filling Shapes
You can fill shape controls using the shape’s FillStyle property with crosshatching, diagonal lines, and other
fill patterns. Here’s a list of the possible values for the FillStyle property:
• VbFSSolid—0; solid
• VbFSTransparent—1 (the default); transparent
• VbHorizontalLine—2; horizontal line
• VbVerticalLine—3; vertical line
• VbUpwardDiagonal—4; upward diagonal
• VbDownwardDiagonal—5; downward diagonal
• VbCross—6; cross
• VbDiagonalCross—7; diagonal cross
You can see what each of these fill styles looks like in Figure 14.11. Note in particular the transparent fill
style—which really just means that the shape control is not filled. That’s usually the style you use when you
draw shapes in a form to group controls together.
Figure 14.11 The Visual Basic fill styles.
TIP: To set the fill color in a shape control, you can use the FillColor property at both design time and runtime.
To place a value in the FillColor property at runtime, use the Visual Basic RGB function like this, where we fill
a shape with red: Shape1.FillColor = RGB(255, 0, 0).
Drawing A Shape Without The IDE Grid
When you draw shapes in the Visual Basic Integrated Development Environment (IDE), the boundaries of
that control fall along the dotted grid you can see in forms. That grid can help in aligning controls and lines,

but there are times when you want finer control.
To turn off the automatic alignment of controls to the grid as you draw them, follow these steps:
1. Select the Tools|Options menu item.
2. Click the General tab in the Options dialog box.
3. Deselect the box marked Align Controls To Grid.
4. Click on OK to close the Options dialog box.
That’s it. Now you’re free to draw controls as you want them and where you want them, without having your
controls’ boundaries fall on a grid line.
TIP: You can hide the grid by deselecting the Show Grid box in the Options dialog box, as well as reset its
dimensions (the default size of each cell in the grid is 120x120 twips).
Moving Shapes At Runtime
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\452-456.html (1 of 3) [3/14/2001 1:48:54 AM]
Simpo PDF Merge and Split Unregistered Version -
Because shape controls are design elements, there are times you might want to move them around as a
program runs, and you can do that with the control’s Move method:
Shape.Move left, [top, [width, height]]
Besides using Move, you can change a shape’s control Top, Left, Width, and Height properties. Let’s see
an example. Here, we’ll just move four shape controls showing circles around at random in a form. To use
random numbers in Visual Basic, we start with the Randomize statement when the form loads; this
initializes the random number generator:
Private Sub Form_Load()
Randomize
End Sub
Next, add four shape controls, Shape1 to Shape4, showing circles, and a timer, Timer1, to the program,
setting the timer Interval property to 1000 (in other words, 1 second), and adding a Timer event handler:
Private Sub Timer1_Timer()
End Sub
Now in Timer1_Timer(), we move the four circles around at random with the Move method:
Private Sub Timer1_Timer()

Shape1.Move Shape1.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape1.Top _
+ ScaleHeight * (Rnd - 0.5) / 50
Shape2.Move Shape2.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape2.Top _
+ ScaleHeight * (Rnd - 0.5) / 50
Shape3.Move Shape3.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape3.Top _
+ ScaleHeight * (Rnd - 0.5) / 50
Shape4.Move Shape4.Left + ScaleWidth * (Rnd - 0.5) / 50, Shape4.Top _
+ ScaleHeight * (Rnd - 0.5) / 50
End Sub
And that’s all it takes. The result of this code appears in Figure 14.12. When you run the program, the circles
move around at random. The code for this example is located in the circles folder on this book’s
accompanying CD-ROM.
Figure 14.12 Moving shape controls around at random.
TIP: Besides moving shapes, you can hide and show them by setting their Visible property to False and True,
respectively.
Adding A Line Control To A Program
The shape control offers a number of predefined shapes for visual design, but sometimes that’s not enough
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\452-456.html (2 of 3) [3/14/2001 1:48:54 AM]
Simpo PDF Merge and Split Unregistered Version -
(what if the Aesthetic Design Department were to start demanding octagons?). For other cases, there’s the
line control.
The line control does just as its name implies: it draws a line. You can draw lines at design time simply as
you would any other control—just click the Line Control tool in the toolbox, press the mouse button at one
end of the line you want, and drag the mouse to the other end.
The line control’s primary properties are X1, X2, Y1, and Y2, and those values form the coordinates of the
line segment: (X1, Y1) and (X2, Y2). You can even change those values at runtime to move or resize the line
(line controls do not have a Move method).
You can also draw lines with this control in forms, picture boxes, and in frames. In fact, lines drawn with the
line control stay visible even if its container’s AutoRedraw property is set to False (unless its Visible

property is set to False).
As an example, we’ve drawn a few lines in the form in Figure 14.13 using the line control.
Figure 14.13 Lines drawn with the line control.
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\452-456.html (3 of 3) [3/14/2001 1:48:54 AM]
Simpo PDF Merge and Split Unregistered Version -
Drawing Thicker, Dotted, And Dashed Lines
Using the line control, you can select a line style with the BorderStyle property. Here are the possible
values for the line control’s BorderStyle property:
• vbTransparent—0; transparent
• vbBSSolid—1 (the default); solid
• vbBSDash—2; dash
• vbBSDot—3; dot
• vbBSDashDot—4; dash-dot
• vbBSDashDotDot—5; dash-dot-dot
• vbBSInsideSolid—6; inside solid
To set a line’s width, you use the BorderWidth property (the default value is 1). It seems a little odd to
call the line’s style BorderStyle and its width BorderWidth—after all, what is the line a border to?
However, those properties are named that way to be consistent with the shape control.
TIP: We might also note that the effect of setting the BorderStyle property depends on the setting of the
BorderWidth property; if BorderWidth isn’t 1 and BorderStyle isn’t 0 or 6, Visual Basic sets
BorderStyle to 1.
Drawing A Line Without The IDE Grid
When you draw lines in the Visual Basic Integrated Development Environment (IDE), those lines fall
along the dotted grid you can see in forms. That grid can help in aligning controls and lines, but there
are times when you want finer control.
To turn off the automatic alignment of controls to the grid as you draw them, follow these steps:
1. Select the Tools|Options menu item.
2. Click the General tab in the Options dialog box.
3. Deselect the box marked Align Controls To Grid.

4. Click on OK to close the Options dialog box.
That’s it. Now you’re free to draw controls as you want them and where you want them, without
having your controls’ boundaries fall on a grid line.
TIP: You can hide the grid by deselecting the Show Grid box in the Options dialog box, as well as reset
its dimensions (the default size of each cell in the grid is 120x120 twips).
Changing A Line Control At Runtime
You can move Visual Basic controls at runtime—why not line controls? You can’t use the Move
method to move a line control at runtime, but you can move or resize it by altering its X1, X2, Y1, and
Y2 properties.
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\456-460.html (1 of 4) [3/14/2001 1:49:12 AM]
Simpo PDF Merge and Split Unregistered Version -
Let’s see an example. In this case, we’ve added four random line controls to a form in a control array,
LineControl(0) to LineControl(3). When the user clicks a command button, Command1, we loop
over all four lines and arrange them horizontally.
Here’s what the code looks like (the measurements are in the Visual Basic default, twips, or 1/1440s of
an inch):
Private Sub Command1_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To 3
LineControl(intLoopIndex).X1 = 1000
LineControl(intLoopIndex).X2 = 3500
LineControl(intLoopIndex).Y1 = 1000 + 100 * intLoopIndex
LineControl(intLoopIndex).Y2 = LineControl(intLoopIndex).Y1
Next intLoopIndex
End Sub
The result of this code appears in Figure 14.14. Now we’re moving lines around at runtime.
Figure 14.14 Changing line controls at runtime.
Using Form Methods To Draw Lines
We’ve seen how to draw lines with the line control—but you can use a form method, the Line method,

to draw lines directly. The Line method can be an important part of graphic design (especially if you
want to draw lines in a loop and don’t want to create a dozen or more line controls), and because we’re
covering that topic in this chapter, we’ll look at the line control here.
Here’s how you use the Line method:
[Form.]Line [(x1, y1)]&45;(x2, y2)[, color]
Let’s see an example. Here, we’ll just draw four lines with the Line method when a form first loads. As
with other graphic methods, to use this method in the Form_Load() handler, you must set the form’s
AutoRedraw property to True.
Here’s the code we add to the Load event, making use of the Line method:
Private Sub Form_Load()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To 3
Line (1000, 1000 + 400 * intLoopIndex)-(3500, 1000 + 400 _
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\456-460.html (2 of 4) [3/14/2001 1:49:12 AM]
Simpo PDF Merge and Split Unregistered Version -
* intLoopIndex)
Next intLoopIndex
End Sub
The result of the preceding code appears in Figure 14.15—you can see the four lines we’ve drawn
there.
Figure 14.15 Drawing lines with the Line method.
TIP: The Line method is often a better choice than line controls if you have a large number of evenly
spaced lines to draw, such as when you need to draw a grid or rules. Note, however, that if the user
resizes the containing form, you might have to redraw those lines.
Using Form Methods To Draw Circles
We’ve seen that you can use the shape control to draw circles, but there is also a form method to do the
same thing: the Circle method.
Here’s how you use the Circle method:
[Form.]Circle (x, y), radius[, color]

For example, here’s how we draw a few circles in a form using the Circle method (note that as with all
graphics methods used in the Form_Load() event handler, you must set the form’s AutoRedraw
property to True here):
Private Sub Form_Load()
Dim intLoopIndex As Integer
For intLoopIndex = 1 To 4
Circle (2300, 500 + 400 * intLoopIndex), 400 * intLoopIndex
Next intLoopIndex
End Sub
Running this code yields the result you see in Figure 14.16. Now we’re drawing circles using the
form’s Circle method.
Figure 14.16 Drawing circles with the Circle method in a form.
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\456-460.html (3 of 4) [3/14/2001 1:49:12 AM]
Simpo PDF Merge and Split Unregistered Version -
Visual Basic 6 Black Book:The Frame, Label, Shape, And Line Controls
http://24.19.55.56:8080/temp/ch14\456-460.html (4 of 4) [3/14/2001 1:49:12 AM]
Simpo PDF Merge and Split Unregistered Version -
Chapter 15
Toolbars, Status Bars, Progress Bars, And
Coolbars
If you need an immediate solution to:
Adding A Toolbar To A Form
Aligning Toolbars In A Form
Adding Buttons To A Toolbar
Handling Toolbar Buttons Clicks
Connecting Toolbar Buttons To Menu Items
Adding Separators To A Toolbar
Adding Images To Toolbar Buttons
Adding Check (Toggle) Buttons To A Toolbar

Creating Button Groups In A Toolbar
Adding Combo Boxes And Other Controls To A Toolbar
Setting Toolbar Button Tool Tips
Letting The User Customize The Toolbar
Adding Toolbar Buttons At Runtime
Adding A Status Bar To A Program
Aligning Status Bars In A Form
Adding Panels To A Status Bar
Displaying Text In A Status Bar
Displaying Time, Dates, And Key States In A Status Bar
Customizing A Status Bar Panel’s Appearance
Displaying Images In A Status Bar
Handling Panel Clicks
Adding New Panels To A Status Bar At Runtime
Creating Simple Status Bars
Adding A Progress Bar To A Form
Using A Progress Bar
Adding A Coolbar To A Form
Aligning Coolbars In A Form
Adding Bands To A Coolbar
Adding Controls To Coolbar Bands
Handling Coolbar Control Events
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\463-469.html (1 of 4) [3/14/2001 1:49:33 AM]
Simpo PDF Merge and Split Unregistered Version -
In Depth
In this chapter, we’re going to take a look at the bar controls: toolbars, status bars, progress bars, and
coolbars. All these controls have their uses in Visual Basic programs, and users are coming to expect
them more and more. We’ll start with an overview of these controls.
Toolbars

Every Windows user knows about toolbars: they’re those bars at the top of a window (although they
can appear other places as well) that are filled with buttons and, sometimes, other controls like combo
bars.
Often, a toolbar contains buttons that correspond to items in an application’s menu, providing an easy
interface for the user to reach frequently used functions and commands. In this way, toolbars can make
life a lot easier for the user. The user can also customize toolbars: double-clicking a toolbar at runtime
opens the Customize Toolbar dialog box, which allows the user to hide, display, or rearrange toolbar
buttons.
You create a toolbar by adding a toolbar control to a form, and to do that, you select the
Project|Components menu item, then click the Controls tab in the Components dialog box, select the
Microsoft Windows Common Controls item, and click on OK to close the Components dialog box.
This adds the Toolbar Control tool to the Visual Basic toolbox, as shown in Figure 15.1; the Toolbar
tool is the twelfth tool down on the left.
Figure 15.1 The Toolbar Control tool.
To add buttons to a toolbar, you add Button objects to its Buttons collection, usually by working with
the toolbar’s property pages. Each button can have text and/or an image, (supplied by an associated
ImageList control). Set text with the Caption property and an image with the Image property for each
Button object. At runtime, you can add or remove buttons from the Buttons collection using Add and
Remove methods.
Status Bars
Status bars appear at the bottom of windows and usually hold several panels in which you can display
text. The status bar is there to give feedback to the user on program operation, as well as other items
like time of day or key states (such as the Caps Lock or the Ins key). Although status bars usually
display text in panels, there is a simple status bar style that makes the status bar function as one long
panel, as we’ll see.
Status bars are built around the Panels collection, which holds the panels in the status bar. Up to 16
Panel objects can be contained in the collection. Each object can display an image and text, as shown
later in this chapter. You can change the text, images, or widths of any Panel object, using its Text,
Picture, and Width properties. To add Panel objects at design time, right-click the status bar, and click
Properties to display the Property Pages dialog box. (We’ll cover the procedure in more detail later in

Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\463-469.html (2 of 4) [3/14/2001 1:49:33 AM]
Simpo PDF Merge and Split Unregistered Version -
the chapter.)
You add the Status Bar Control tool to the toolbox by following the same steps to add the Toolbar
Control tool, because the status bar control is also part of the Microsoft Windows common controls.
The Status Bar Control tool is the twelfth tool down on the right in Figure 15.2.
Figure 15.2 The Status Bar Control tool.
Progress Bars
Progress bars give the user some visual feedback on what’s happening during a time-consuming
operation. They present the user with a color bar that grows in the control to show how the operation is
proceeding, usually from 0 to 100 percent. You can use a progress bar when an operation will take
some time to finish. The progress bar’s Value property (not available at design time) determines how
much of the control has been filled. The Min and Max properties set the limits of the control.
You add the Progress Bar Control tool to the toolbox by following the same steps to add the toolbar
tool, because the progress bar control is also part of the Microsoft Windows common controls. The
Progress Bar Control tool is the thirteenth tool down on the left in Figure 15.3.
Figure 15.3 The Progress Bar Control and the Coolbar Control tools.
Coolbars
Coolbars were first introduced in the Microsoft Internet Explorer, and they are toolbars that present
controls in bands. Users can adjust these bands by dragging a gripper, which appears at left in a band.
In this way, users can configure the coolbar by sliding the bands around as they want. One popular use
of coolbars is to display toolbars in the bands of that coolbar, allowing users to move those toolbars
around as they want.
The Coolbar Control tool is on the bottom, at left, in the Visual Basic toolbox in Figure 15.3. These
controls can act just as toolbars do, as we’ll see.
That’s it for the overview—it’s time to turn to the Immediate Solutions.
Immediate Solutions
Adding A Toolbar To A Form
The Testing Department is calling again. Your program, SuperDuperTextPro, is wonderful—but what

about putting in a toolbar? That would make things easier for the program’s users, because they could
click buttons in the toolbar instead of having to open menu items. So how do you add a toolbar to a
form?
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\463-469.html (3 of 4) [3/14/2001 1:49:33 AM]
Simpo PDF Merge and Split Unregistered Version -
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\463-469.html (4 of 4) [3/14/2001 1:49:33 AM]
Simpo PDF Merge and Split Unregistered Version -
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. We’ll 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.
Figure 15.4 The Application Wizard Customize Toolbar dialog box.
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.
Figure 15.5 An Application Wizard program, complete with toolbar.
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. Here’s how that works:
1. Select the Project|Components menu item.
2. Click the Controls tab in the Components dialog box.
3. Select the Microsoft Windows Common Controls item, and click on OK to close the
Components dialog box.
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 you’ve got a new toolbar—but 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 Form

Now that you’ve 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:
• vbAlignNone—0
• vbAlignTop—1 (the default)
• vbAlignBottom—2
• vbAlignLeft—3
• vbAlignRight—4
Adding Buttons To A Toolbar
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\469-474.html (1 of 4) [3/14/2001 1:49:49 AM]
Simpo PDF Merge and Split Unregistered Version -
You’ve 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 toolbar’s property pages open, click the Buttons
tab, as shown in Figure 15.6.
Figure 15.6 Adding new buttons to a toolbar.
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 you’re done, click on the OK button to close the toolbar’s property pages. Now that you’ve
installed buttons in your toolbar, how do you handle button clicks? Take a look at the next topic.
Handling Toolbar Buttons Clicks
Now that you’ve set up your toolbar with the buttons you want, how can you make those buttons
active? You do that with the toolbar control’s 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 button’s 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.
Figure 15.7 Determining which button the user has clicked.
Besides using the Index property, you can also give each button’s Key property a text string (you do
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\469-474.html (2 of 4) [3/14/2001 1:49:49 AM]
Simpo PDF Merge and Split Unregistered Version -
that at design time in the toolbar control’s 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 book’s accompanying CD-ROM.
Connecting Toolbar Buttons To Menu Items
You 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 item’s 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 item’s 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
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\469-474.html (3 of 4) [3/14/2001 1:49:49 AM]
Simpo PDF Merge and Split Unregistered Version -
Case "CloseFile"
mnuFileClose_Click
End Select
End Sub
And that’s all it takes. Now we’ve connected toolbar buttons to menu items.

Adding Separators To A Toolbar
The Aesthetic Design Department is calling again. Can’t 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 way—by using separators. In menus, separators appear as solid lines, but
in toolbars, separators just appear as blank spaces, setting groups of buttons apart.
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\469-474.html (4 of 4) [3/14/2001 1:49:49 AM]
Simpo PDF Merge and Split Unregistered Version -
Let’s see an example. Insert a new button into a toolbar and set its Style property to tbrSeparator, as
shown in Figure 15.8.
Figure 15.8 Adding a spacer to a toolbar.
Now add other buttons, and click on OK to close the toolbar’s property pages. When you do, you’ll see
that the separator puts some distance between the buttons, as shown in Figure 15.9.
Figure 15.9 Using a separator in a toolbar.
TIP: Although toolbar separators just look like blank space, they count as buttons, which means that
they have their own Index value. That means that you have to take separators into account when figuring
a button’s Index value in your toolbar in order to handle it when it’s clicked.
Adding Images To Toolbar Buttons
The Aesthetic Design Department is calling. Your new toolbar looks great, but it would look even
better if you used images in the buttons and not text captions. How about it?
You can give toolbar buttons if you place those images into an image list control. Image lists are
Windows common controls just as toolbars are, so add an image list to a program now.
To place the images you want in the buttons in the image list, follow these steps:
1. Right-click the image list control.
2. Select the Properties menu item.
3. Click the Images tab in the image control’s property pages.
4. Click the Insert Picture button to insert the first image (you can browse through your hard
disks and select the images you want).
5. Keep going until all the images have been added to the image control, then click on OK to
close the property pages.

Now you need to associate the image control with the toolbar, and you do that in the toolbar’s property
pages; just follow these steps:
1. Right-click the toolbar and select the Properties item to open the toolbar’s property pages, as
shown in Figure 15.10.
Figure 15.10 Adding images from an image control to a toolbar.
2. Next, click the Buttons tab in the property pages, as shown in Figure 15.11.
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\474-480.html (1 of 4) [3/14/2001 1:50:18 AM]
Simpo PDF Merge and Split Unregistered Version -
Figure 15.11 Connecting images from an image control to toolbar buttons.
3. Enter the index of the image in the image control you want to connect to the first button in the
box labeled Image (image lists are 1-based).
4. Keep going for the other buttons, entering the image control indices of the images you want to
connect to those buttons.
5. Click on OK to close the property pages.
When you run the program, the images appear in the toolbar.
You can also connect an image control to a toolbar at runtime, using the toolbar’s ImageList property:
Private Sub Command1_Click()
Toolbar1.ImageList = ImageList1
End Sub
TIP: Visual Basic comes with the standard bitmaps you’ll find in Windows toolbars—just check the
common\graphics\bitmaps\offctlbr\small\color directory.
Adding Check (Toggle) Buttons To A Toolbar
The Testing Department is calling again: The toolbar you’ve added to your program,
SuperDuperTextPro, is terrific, but there’s one problem. One of the menu items, the Insert item,
displays a checkmark next to it when the user toggles that mode on. Can’t you add a checkmark to the
Insert button in the toolbar as well?
The way toolbars handle this problem instead of displaying checkmarks is to keep a button depressed
once it’s been pressed. In this way, you can show toggle states. Let’s take a look at an example.
To make a toolbar button a “check” button, you must set its Style property to tbrCheck, and you do

that in the toolbar’s property pages. Right-click the toolbar now and select the Properties item to open
the property pages. Click the Buttons tab in the property pages, as shown in Figure 15.12.
Figure 15.12 Making a toolbar button a check button.
Select the button you want to work with, and set its style to tbrCheck, as shown in Figure 15.12.
That’s it. Now when the user clicks the button, it stays clicked, as shown in Figure 15.13, until the user
clicks it again.
Figure 15.13 A check toolbar button at work.
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\474-480.html (2 of 4) [3/14/2001 1:50:18 AM]
Simpo PDF Merge and Split Unregistered Version -
Creating Button Groups In A Toolbar
You may notice in some toolbars that a set of buttons are mutually exclusive—for example, if your
word processor lets you align text to the right, left, and center with buttons in a toolbar, only one of
those styles can be active at once. When the user clicks one, the others should toggle off.
You can set up groups of mutually exclusive buttons in toolbars, just as you can with groups of option
buttons (in fact, that’s just what button groups in a toolbar resemble: a group of graphical [Style = 1]
option buttons).
To create a button group, just follow these steps:
1. Open the toolbar’s property pages by right-clicking the toolbar and selecting the Properties
item.
2. Click the Buttons tab.
3. Select the button in the button group, and set its style to tbrButtonGroup in the Style box, as
shown in Figure 15.14.
Figure 15.14 Creating a button group in a toolbar.
4. Repeat Step 3 for the other buttons in the button group.
5. Click on OK to close the property pages.
That’s all it takes. Now the buttons you’ve placed together in a group will act together. When the user
clicks one to select it, the others will toggle off (in other words, go back to their unselected position).
Button groups can be very useful in a toolbar—any time option buttons would come in handy in a
toolbar, just use a button group instead.

Adding Combo Boxes And Other Controls To A Toolbar
The Program Design Department is calling again. That shopping program you’ve written,
SuperDuperGroceryStore4U, is nice, but what about listing the available groceries in a combo box in
the toolbar. You wonder, how can you do that?
You can add combo boxes or other controls to a toolbar easily; just set aside space in the toolbar by
setting a button’s Style property to tbrPlaceholder. Here are the steps to follow to add a combo box to
a toolbar:
1. Right-click the toolbar, and select Properties in the menu that appears.
2. Click the Buttons tab in the property pages that open, as shown in Figure 15.15.
Figure 15.15 The toolbar property pages.
3. Insert a new button where you want the combo box to go.
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\474-480.html (3 of 4) [3/14/2001 1:50:18 AM]
Simpo PDF Merge and Split Unregistered Version -
4. Set the new button’s Style property to tbrPlaceholder in the box labeled Style. This means
the button won’t appear—there’ll only be a blank space, and we’ll place our combo box there.
5. Set the width of the space you want to leave for the combo box by entering a twip (1/1440s of
an inch) value in the box labeled Width: (Placeholder), as shown in Figure 15.15.
6. Close the property pages by clicking on OK.
7. Click the Combo Box Control tool in the toolbox, and draw a new combo box in the new
space in the toolbar.
8. Add the items you want in the combo box in the Properties window’s List property (or add
items to the combo box at runtime).
9. Connect the code you want to the combo box. For example, here we respond to combo box
clicks and text entry by displaying a message box:
Private Sub Combo1_Change()
MsgBox "You entered " & Combo1.Text
End Sub
Private Sub Combo1_Click()
MsgBox "You selected " & Combo1.Text

End Sub
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\474-480.html (4 of 4) [3/14/2001 1:50:18 AM]
Simpo PDF Merge and Split Unregistered Version -
That’s all we need—now run the program, as shown in Figure 15.16.
Figure 15.16 Adding a combo box to a toolbar.
When users make a selection with the combo box, we display a message box letting them know what
they’ve selected. Our combo box toolbar example is a success.
The code for this example appears in the combotoolbar folder on this book’s accompanying CD-ROM.
Setting Toolbar Button Tool Tips
Giving toolbar buttons tool tips (those small yellow windows that display explanatory text when the
mouse cursor rests on the underlying control) is an easy process. All you need to do to give a button a
tool tip is to set its ToolTipText property.
To set the ToolTipText property, right-click the toolbar and select the Properties item in the menu that
opens. Click the Buttons tab and select the button you want to add the tool tip to. Place the tool tip text
in the box labeled ToolTipText, as shown in Figure 15.17. Finally, close the property pages by clicking
on OK. Now when you run the program, the button displays a tool tip, as shown in Figure 15.18.
Figure 15.17 Setting a toolbar button’s tool tip text.
Figure 15.18 Toolbar buttons with tool tips.
Letting The User Customize The Toolbar
The Testing Department has sent you a memo. Some users of your new program, SuperDuperTextPro,
want the Save button at left in the toolbar, but other users want the Create New Document button there.
What can we do?
You can let the user customize the toolbar. Just set the AllowCustomize property to True (the default).
When the user double-clicks the toolbar, the Customize Toolbar dialog box appears, as shown in Figure
15.19. Users can customize the toolbar as they like using that dialog box.
Figure 15.19 Using the Customize Toolbar dialog box.
TIP: If you allow your end user to reconfigure the toolbar control, you can save and restore the toolbar
by using the SaveToolbar and RestoreToolbar methods.
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars

http://24.19.55.56:8080/temp/ch15\480-485.html (1 of 4) [3/14/2001 1:50:55 AM]
Simpo PDF Merge and Split Unregistered Version -
Adding Toolbar Buttons At Runtime
How do you add buttons to a toolbar at runtime? It’s possible to add menu items to menus, so it should
be possible to add buttons to toolbars.
It is. To add a new button when the user clicks a button, we start by declaring a new Button object:
Private Sub Command1_Click()
Dim Button1 As Button

End Sub
Next, we add a new button to the toolbar’s Buttons collection, which is how it stores its buttons
internally. As with all collections, the Buttons collection has an Add method, and we use it here:
Private Sub Command1_Click()
Dim Button1 As Button
Set Button1 = Toolbar1.Buttons.Add()

End Sub
Now we’re free to set the button’s style. Here, we make it a standard button by setting its Style
property to tbrDefault (other options include tbrButtonGroup, tbrSeparator, tbrCheck,
tbrPlaceHolder, and tbrDropDown):
Private Sub Command1_Click()
Dim Button1 As Button
Set Button1 = Toolbar1.Buttons.Add()
Button1.Style = tbrDefault

End Sub
We can also give the new button a caption:
Private Sub Command1_Click()
Dim Button1 As Button
Set Button1 = Toolbar1.Buttons.Add()

Button1.Style = tbrDefault
Button1.Caption = "New button"

End Sub
Finally, we give the new button a tool tip:
Private Sub Command1_Click()
Dim Button1 As Button
Visual Basic 6 Black Book:Toolbars, Status Bars, Progress Bars, And Coolbars
http://24.19.55.56:8080/temp/ch15\480-485.html (2 of 4) [3/14/2001 1:50:55 AM]
Simpo PDF Merge and Split Unregistered Version -

×