1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual Basic 6 Black Book phần 5 docx

112 332 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề The Frame, Label, Shape, And Line Controls
Tác giả Steven Holzner
Trường học The Coriolis Group
Thể loại sách
Năm xuất bản 1998
Định dạng
Số trang 112
Dung lượng 3,38 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

For more on this control, see the other topics in this chapter.You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor

Trang 1

" VbShapeOval2; oval

" VbShapeCircle3; circle

" VbShapeRoundedRectangle4; rounded rectangle

" VbShapeRoundedSquare5; rounded square

One important use of shape controls is to group other controls together (Note, however, that shapecontrols cant act as true control containers in the way picture boxes or frames can For example, youcant group option buttons together with shapes.) In Figure 14.10, were using shape controls to groupthe buttons visually into two groups

Figure 14.10 Using the shape control to group other controls

You can also set the width of the shapes 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

shapes drawing line, including using dots and dashes For more on this control, see the other topics inthis chapter

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 - http://www.simpopdf.com

Trang 2

To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book

(Publisher: The Coriolis Group)

Author(s): Steven Holzner

the controls Shape property to VbShapeRectangle (thats the default anyway).

Thats all you have to dothe shape control is very easy to work with Using the shapecontrol, you can draw in both forms and picture boxes

You can also set the width of the shapes 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 shapes drawing line, including

using dots and dashes

Drawing Squares

How do you draw squares with the shape control? You start by clicking the ShapeControl tool in the Visual Basic toolbox and drawing that control to match the sizeand location you want your new figure to have To draw a square, you simply set the

controls Shape property to VbShapeSquare.

Thats all you have to dothe shape control is simple Using the shape control, you candraw in both forms and picture boxes

You can also set the width of the shapes 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 shapes drawing line, including

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 - http://www.simpopdf.com

Go!

Trang 3

using dots and dashes For more on this control, see the other topics in this chapter.

You can also set the width of the shapes 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 shapes drawing line, including

using dots and dashes For more on this control, see the other topics in this chapter

You can also set the width of the shapes 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 shapes 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 clickingthe Shape Control tool in the Visual Basic toolbox and drawing that control to matchthe size and location you want your new figure to have To draw a rounded rectangle,

you simply set the controls Shape property to VbShapeRoundedRectangle.

Thats all you have to dothis control is very easy Using the shape control, you candraw in both forms and picture boxes

You can also set the width of the shapes 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 shapes drawing line, including

using dots and dashes For more on this control, see the other topics in this chapter

Drawing Rounded Squares

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 - http://www.simpopdf.com

Trang 4

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 sizeand location you want your new figure to have To draw a rounded square, you simply

set the controls Shape property to VbShapeRoundedSquare.

Thats all you have to do The shape control is easy Using the shape control, you candraw in both forms and picture boxes

You can also set the width of the shapes 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 shapes 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 Cant you do something about the

shapes in your program? Maybe make themdotted? You think, dotted?

Visual Basic can help here Just set the shape controls BorderStyle property Here are the possible values for the BorderStyle property:

border width (in other words, the drawing line width) using the shape controls

BorderWidth property Just set that property to the new value you want for the

border thickness (the default value is 1)

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 - http://www.simpopdf.com

Trang 5

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 fillstyle—which really just means that the shape control is not filled That’s usually the style you use when youdraw 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 ofthat 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 yourcontrols’ 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

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 - http://www.simpopdf.com

Trang 6

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()

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 circlesmove 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 enoughhttp://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 - http://www.simpopdf.com

Trang 7

(what if the Aesthetic Design Department were to start demanding octagons?) For other cases, there’s theline control.

The line control does just as its name implies: it draws a line You can draw lines at design time simply asyou would any other control—just click the Line Control tool in the toolbox, press the mouse button at oneend 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

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 - http://www.simpopdf.com

Trang 8

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:

• 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 fallalong the dotted grid you can see in forms That grid can help in aligning controls and lines, but thereare 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.

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 - http://www.simpopdf.com

Trang 9

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

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’recovering 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

Trang 10

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.

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 - http://www.simpopdf.com

Trang 11

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 - http://www.simpopdf.com

Trang 12

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

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 - http://www.simpopdf.com

Trang 13

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 expectthem 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 theycan appear other places as well) that are filled with buttons and, sometimes, other controls like combobars

Often, a toolbar contains buttons that correspond to items in an application’s menu, providing an easyinterface for the user to reach frequently used functions and commands In this way, toolbars can makelife a lot easier for the user The user can also customize toolbars: double-clicking a toolbar at runtimeopens the Customize Toolbar dialog box, which allows the user to hide, display, or rearrange toolbarbuttons

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 theMicrosoft 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 Toolbartool 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 displaytext The status bar is there to give feedback to the user on program operation, as well as other itemslike time of day or key states (such as the Caps Lock or the Ins key) Although status bars usuallydisplay text in panels, there is a simple status bar style that makes the status bar function as one longpanel, 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

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 - http://www.simpopdf.com

Trang 14

the chapter.)

You add the Status Bar Control tool to the toolbox by following the same steps to add the ToolbarControl 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 isproceeding, 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 toolbartool, because the progress bar control is also part of the Microsoft Windows common controls TheProgress 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 toolbarsaround as they want

The Coolbar Control tool is on the bottom, at left, in the Visual Basic toolbox in Figure 15.3 Thesecontrols 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 couldclick buttons in the toolbar instead of having to open menu items So how do you add a toolbar to aform?

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 - http://www.simpopdf.com

Trang 15

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 - http://www.simpopdf.com

Trang 16

You use the toolbar control In fact, probably the easiest way to add a toolbar to a program is to designthat program with the Visual Basic Application Wizard We’ll take a look at what the ApplicationWizard 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, yousee 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 atoolbar 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 atoolbar 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:

Trang 17

You’ve got your new toolbar in the form you want and aligned it correctly How about adding somebuttons?

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 Buttonstab, 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 Buttonbutton) When you add a new button to a toolbar, you can associate a picture or caption with it Forexample, 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’veinstalled 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 inFigure 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

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 - http://www.simpopdf.com

Trang 18

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

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

Trang 19

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 intological 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

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 - http://www.simpopdf.com

Trang 20

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 seethat 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 evenbetter 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 propertypages; 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.

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 - http://www.simpopdf.com

Trang 21

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 theInsert button in the toolbar as well?

The way toolbars handle this problem instead of displaying checkmarks is to keep a button depressedonce 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 openthe 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 userclicks it again

Figure 15.13 A check toolbar button at work.

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 - http://www.simpopdf.com

Trang 22

Creating Button Groups In A Toolbar

You may notice in some toolbars that a set of buttons are mutually exclusive—for example, if yourword processor lets you align text to the right, left, and center with buttons in a toolbar, only one ofthose 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 userclicks 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 atoolbar, 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.

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 - http://www.simpopdf.com

Trang 23

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

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 - http://www.simpopdf.com

Trang 24

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 whatthey’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 themouse 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 Figure15.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.

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 - http://www.simpopdf.com

Trang 25

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

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 - http://www.simpopdf.com

Trang 26

Set Button1 = Toolbar1.Buttons.Add()

Button1.Style = tbrDefault

Button1.Caption = "New button"

Button1.ToolTipText = "New button"

Adding A Status Bar To A Program

The Testing Department is calling again Your new SuperDuperDataCrunch program looks good, but

what about the status bar? You ask, what status bar? Exactly, they say

How can you add a status bar to your program? You could design the program with the Visual BasicApplication Wizard, which automatically adds a status bar (see “Adding A Toolbar To A Form” earlier

in this chapter for more information) However, most programmers will want to add their own statusbar to their programs, and you create a status bar by adding a status bar control to a form Here’s howthat 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 Status Bar Control tool to the Visual Basic toolbox, as shown in Figure 15.2 To place astatus bar in your form, just double-click the Status Bar Control

Now you’ve got a new status bar—but how do you align it at the top of the window and display text init? See the next couple of topics in this chapter

Aligning Status Bars In A Form

Now that you’ve added a status bar to your form, where does it go? By default, it aligns itself with the

bottom of the client area of the form You can set the alignment of the status bar with its Align

property, which can take these values:

Trang 27

• vbAlignRight—4

Adding Panels To A Status Bar

Now that you’ve added a status bar to your program, it’s time to take the next step: adding panels to thestatus bar The text in a status bar is displayed in those panels

A status bar control has a Panels collection, and you add the panels you want to that collection To do

that at design time, follow these steps:

1 Right-click the status bar, and select the Properties item in the menu that opens.

2 Click the Panels tab in the property pages, as shown in Figure 15.20.

Figure 15.20 Adding a panel to a status bar.

3 Click the Insert Panel button as many times as you want panels in your status bar.

4 Close the property pages by clicking on OK.

http://24.19.55.56:8080/temp/ch15\480-485.html (4 of 4) [3/14/2001 1:50:55 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 28

It’s also easy to add a new status bar panel at runtime—just use the Panels collection’s Add method.

Here’s an example where we add a panel to a status bar when the user clicks a command button:

Private Sub Command1_Click()

Dim panel5 As Panel

Set panel5 = StatusBar1.Panels.Add()

Panel5.Text = "Status: OK"

End Sub

Now that you’ve added panels to the status bar, how do you display text in those panels? See the nexttopic

Displaying Text In A Status Bar

You’ve added a new status bar to your program and added the panels you want to the status bar—but

how do you display text? The status bar control you’ve added doesn’t seem to have a Text property.

The text in a status bar is displayed in the status bar’s panels (unless the status bar is a simple statusbar—see “Creating Simple Status Bars” later in this chapter—in which case you use the status bar’s

SimpleText property) Displaying text in a status bar’s panels is easy—just select the panel you want

to work with as the index into the status bar’s Panels collection, and use that panel’s Text property.

Here’s an example—in this case, we’ll display the program status, “OK”, in the first panel of the status

bar (note that the Panels collection is 1-based) when the user clicks a command button, Command1:

Private Sub Command1_Click()

StatusBar1.Panels(1).Text = "OK"

End Sub

That’s it—the result of this code appears in Figure 15.21 Now we’ve displayed text in a status bar

Figure 15.21 Displaying text in a status bar control.

The code for this example is located in the statusbar folder on this book’s accompanying CD-ROM

Displaying Time, Dates, And Key States In A Status Bar

The Testing Department has sent you some email: the clock-watchers who use your

SuperDuperDataCrunch program want a clock to watch Can you add one to your program?

You can, and you can display it in the status bar In fact, status bar controls are already set up to displaycommon status items like key states and dates To display one of those items, just right-click the statusbar, select the Properties item in the menu that appears, click the Panels tab, select the panel you want

to work with, and set the Style property in the box labeled Style to one of the following:

• sbrText—0 (the default); text and/or a bitmap Displays text in the Text property.

http://24.19.55.56:8080/temp/ch15\485-489.html (1 of 3) [3/14/2001 1:51:03 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 29

• sbrCaps—1; Caps Lock key Displays the letters “CAPS” in bold when Caps Lock is enabled,

and dimmed when disabled

• sbrNum—2; Num Lock key Displays the letters “NUM” in bold when the Num Lock key is

enabled, and dimmed when disabled

• sbrIns—3; Insert key Displays the letters “INS” in bold when the Insert key is enabled, and

dimmed when disabled

• sbrScrl—4; Scroll Lock key Displays the letters “SCRL” in bold when Scroll Lock is

enabled, and dimmed when disabled

• sbrTime—5; time Displays the current time in the system format.

• sbrDate—6; date Displays the current date in the system format.

• sbrKana—7; Kana lock Displays the letters “KANA” in bold when kana lock is enabled, and

dimmed when disabled (this feature is enabled on Japanese operating systems only)

See Figure 15.22 for a status bar showing the time

Figure 15.22 Displaying time in a status bar.

Customizing A Status Bar Panel’s Appearance

You can customize the appearance of the panels in a status bar with the Bevel, AutoSize, and

Alignment properties The Bevel property specifies whether the panel will have an inset bevel (the default), raised, or none at all Here’s how you can set the Bevel property:

• sbrNoBevel—0; the Panel displays no bevel, and text looks like it is displayed right on the

status bar

• sbrInset—1; the Panel appears to be sunk into the status bar.

• sbrRaised—2; the Panel appears to be raised above the status bar.

The AutoSize property determines how a panel will resize itself when its container (usually a form) is resized by the user Here are the settings for the AutoSize property:

• sbrNoAutoSize—0; None No autosizing occurs The width of the panel is always and exactly that specified by the Width property.

• sbrSpring—1; Spring When the parent form resizes and there is extra space available, all

panels with this setting divide the space and grow accordingly (The panels’ width never falls

below that specified by the MinWidth property.)

• sbrContents—2; Content The panel is resized to fit its contents.

The Alignment property indicates how the text or image in a panel will align in the panel The settings for the Alignment property are as follows:

• sbrLeft—0; text appears left-justified and to the right of any bitmap.

• sbrCenter—1; text appears centered and to the right of any bitmap.

• sbrRight—2; text appears right-justified but to the left of any bitmap.

http://24.19.55.56:8080/temp/ch15\485-489.html (2 of 3) [3/14/2001 1:51:03 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 30

Displaying Images In A Status Bar

The Aesthetic Design Department is on the phone How about adding a few images to the status bar? Infact, how about some animation for the user to watch while the program does other things? You think,

is that possible?

Yes, it is, because status bar panels have a Picture property To place an image in a status bar panel at

design time, follow these steps:

1 Right-click the status bar, and select the Properties item in the menu that appears.

2 Click the Panels tab in the property pages that open.

3 Select the panel you want to work with.

4 Set the panel’s Picture property by clicking the Browse button in the box labeled Picture.

You can set this property with an image file on disk

5 Close the property pages by clicking on OK.

That’s it—now when you run the program, the image you’ve selected appears in the panel you’vechosen, as shown in Figure 15.23

Figure 15.23 Displaying images in a status bar.

http://24.19.55.56:8080/temp/ch15\485-489.html (3 of 3) [3/14/2001 1:51:03 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 31

You can also set a status bar panel’s image at runtime For example, here’s how we set the image in thefirst panel of a status bar, using the image in a picture box when the user clicks a button (you can also

use the LoadPicture function to load images in directly):

Private Sub Command1_Click()

StatusBar1.Panels(1).Picture = Picture1.Picture

End Sub

TIP: You can even create animation in a status bar panel; just set up a timer and place a succession of

images in the panel’s Picture property.

Handling Panel Clicks

Are status bars static controls? Or can they handle events? Status bars certainly can handle events, and

the most common are PanelClick and PanelDblClick The event handler procedures for those events

are passed the panel that was clicked, as in this example:

Private Sub StatusBar1_PanelClick(ByVal Panel As ComctlLib.Panel)

End Sub

You can tell which panel was clicked by checking the Panel argument’s Index or Key properties For example, here’s how we use the Index property to report to the user which panel was clicked:

Private Sub StatusBar1_PanelClick(ByVal Panel As ComctlLib.Panel)

MsgBox "You clicked panel " & Panel.Index

End Sub

If you’ve set the Key properties of the panels in your status bar (the Key property holds a text string), you can set up a Select Case statement to see which panel was clicked and take the appropriate action:

Private Sub StatusBar1_PanelClick(ByVal Panel As ComctlLib.Panel)

Select Case Panel.Key

Adding New Panels To A Status Bar At Runtime

It’s easy to add a new status bar panel at runtime—just use the Panels collection’s Add method Here’s

an example where we add a panel to a status bar when the user clicks a command button:

http://24.19.55.56:8080/temp/ch15\489-493.html (1 of 4) [3/14/2001 1:51:11 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 32

Private Sub Command1_Click()

Dim panel5 As Panel

Set panel5 = StatusBar1.Panels.Add()

Panel5.Text = "Status: OK"

End Sub

Creating Simple Status Bars

There’s a way of using a status bar without using panels: by making the status bar a simple status bar

How do you make a status bar into a simple status bar? You set its Style property to sbrSimple (which equals 1; the other option is sbrNormal, which equals 0) Simple status bars have only one panel, and you set the text in that panel with the SimpleText property.

Here’s an example; in this case, we just display the message “Status: OK” in the simple status bar whenthe user clicks a button:

Private Sub Command1_Click()

StatusBar1.SimpleText = "Status: OK"

End Sub

The result of this code appears in Figure 15.24

Figure 15.24 Using a simple status bar.

TIP: One reason programmers used to use simple status bars was to show the progress of an operation

by displaying a succession of dots (or other text) in the status bar’s single long panel However, you can

use the progress bar control for that these days—see the next topic in this chapter.

Adding A Progress Bar To A Form

The Testing Department is calling again Why does downloading the 200MB data file your programrequires take so long? Well, you explain, the Internet is like that They ask, but can’t you at least showthe user what progress the downloading operation is making? You take a look at the Progress Bar

Control tool in the Visual Basic toolbox Sure, you say, no problem

You can use progress bar controls to show the progress of a time-consuming operation These controlsdisplay a colored band that can grow (or shrink) as time goes on To add a progress bar to a form,

follow these steps:

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 Progress Bar Control tool to the Visual Basic toolbox, asshown in Figure 15.3

http://24.19.55.56:8080/temp/ch15\489-493.html (2 of 4) [3/14/2001 1:51:11 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 33

4 To place a progress bar in your form, just add it as you would any control, using the Progress

Bar Control tool

5 Set the progress bar’s Min (default is 0) and Max (default is 100) properties as desired to

match the range of the operation you’re reporting on

Now you’ve got a new progress bar in your form—but how do you use it? See the next topic

Using A Progress Bar

Now that you’ve added a progress bar to your program and set its Min and Max properties, how do you actually use it to display data? You use a progress bar’s Value property (available only at runtime)

to specify how much of the progress bar is visible As you might expect, setting Value to Min means none of the progress bar is visible, and setting it to Max means all of it is.

Let’s see an example In this case, we’ll let the user click a button to display a progress bar whose bar

lengthens from Min to Max in 10 seconds Add a progress bar, command button, and a timer control to

a form now Set the timer’s Interval property to 1000 (in other words, 1000 milliseconds, or 1 second) We’ll leave the progress bar’s Min property at 0 and its Max property at 100, the defaults.

When the form loads, we disable the timer and set the progress bar’s Value to 0:

Private Sub Form_Load()

Timer1.Enabled = False

ProgressBar1.Value = 0

End Sub

When the user clicks the command button, we want to start the progress bar, so we enable the timer

We also set the progress bar back to 0 (even though we did that when the form loads, the user mightwant to restart the operation, which means he might click the button several times):

Private Sub Command1_Click()

ProgressBar1.Value = 0

Timer1.Enabled = True

End Sub

Finally, in the Timer event handler, Timer1_Timer, we add a value of 10 to the progress bar’s Value

property every second We also check if we’ve filled the progress bar, and if so, disable the timer:

Private Sub Timer1_Timer()

Trang 34

Figure 15.25 Using a progress bar.

http://24.19.55.56:8080/temp/ch15\489-493.html (4 of 4) [3/14/2001 1:51:11 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 35

The code for this example is located in the progressbar folder on this book’s accompanying CD-ROM.

Adding A Coolbar To A Form

Coolbars were first introduced in the Microsoft Internet Explorer, and they are toolbars that presentcontrols in bands The user can adjust these bands by dragging a gripper, which appears at left in aband In this way, users can configure the coolbar by sliding the bands around as they want

To add a coolbar control to a form, follow these steps:

1 Select the Project|Components menu item.

2 Click the Controls tab in the Components dialog box.

3 Select the Microsoft Windows Common Controls-3 item, and click on OK to close the

Components dialog box This adds the Coolbar Control tool to the Visual Basic toolbox, asshown in Figure 15.3

4 To place a coolbar in your form, just add it as you would any control, using the Coolbar

Control tool

Now that you’ve added a coolbar to your form, maybe you’ll need to align it in that form? See the nexttopic for the details

Aligning Coolbars In A Form

Now that you’ve added a coolbar to your form, how do you align it to the top, bottom, or wherever you

want to place it? You use the Align property, setting it to one of these values:

• vbAlignNone—0 (the default)

Adding Bands To A Coolbar

The controls in a coolbar are usually organized into bands (and note that those controls can themselvescontain controls, as when you place toolbars in a band) To add a band to a coolbar, just follow thesesteps:

1 Right-click the coolbar and select the Properties item in the menu that appears.

2 Click the Bands tab in the coolbar’s property pages, as shown in Figure 15.26.

Figure 15.26 The coolbar property pages.

http://24.19.55.56:8080/temp/ch15\493-497.html (1 of 3) [3/14/2001 1:51:16 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 36

3 Add new bands to the coolbar using the Insert Band button.

4 When finished, close the property pages by clicking on OK.

You can also add a band to a coolbar at runtime with its Bands collection, because that collection supports the usual collection methods Add and Remove For example, here’s how we add a new band

to a coolbar at runtime:

Private Sub Command1_Click()

Dim band5 As Band

Set band5 = CoolBar1.Bands.Add()

End Sub

Now that you’ve added bands to a coolbar, how do you install controls in those bands? Take a look atthe next topic to get the details

Adding Controls To Coolbar Bands

You add controls to coolbar bands by setting the band’s Child property The Child property can only

hold one child control, which you might think limits the power of coolbars, but in fact, that control can

be a complete toolbar If you fill a coolbar’s bands with toolbar controls, users can arrange and slidethose toolbars around as they like

To add a control to a coolbar band, follow these steps:

1 Add the control (such as a toolbar) you want to place in a band to the coolbar by drawing it

inside the coolbar

2 Right-click the coolbar and select the Properties item in the menu that appears.

3 Click the Bands tab in the coolbar’s property pages, as shown in Figure 15.27.

Figure 15.27 Adding a toolbar to a coolbar band.

4 Select the band you want to work with.

5 Set the band’s Child property to the control you want to add to that band, such as Toolbar1

in Figure 15.27

6 Close the coolbar’s property pages by clicking on OK.

You can also set a band’s Child property at runtime, as in this example where we set the control in the coolbar’s first band to Toolbar1:

Private Sub Command1_Click()

Set CoolBar1.Bands(1).Child = Toolbar1

Trang 37

You’ve set up the coolbar you want and placed a few toolbars in the various bands of that coolbar Nowhow do you handle button clicks in those toolbars (or other controls you’ve place in a coolbar’s bands)?Handling events from controls in coolbar bands is easy—just connect event handlers to those controls

as you normally would (in other words, if they weren’t in a coolbar) Here’s an example where we’ve

added a toolbar, Toolbar1, to a coolbar You can add buttons to the toolbar as you would

normally—just open the toolbar’s property pages and use the Insert Button button To handle Click

events for those button, you just double-click the toolbar’s buttons at design time, which opens the

matching Click event handler:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)End Sub

Then you just proceed as you would in a normal toolbar, such as adding this code where we indicate tousers which button they’ve clicked:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button) MsgBox "You clicked button " & Button.Index

End Sub

http://24.19.55.56:8080/temp/ch15\493-497.html (3 of 3) [3/14/2001 1:51:16 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 38

Chapter 16

Image Lists, Tree Views, List Views, And Tab

Strips

If you need an immediate solution to:

Adding An Image List To A Form

Adding Images To Image Lists

Using The Images In Image Lists

Setting Image Keys In An Image List

Adding A Tree View To A Form

Selecting Tree View Styles

Adding Nodes To A Tree View

Adding Subnodes To A Tree View

Adding Images To A Tree View

Expanding And Collapsing Nodes (And Setting Node Images To Match)

Handling Tree View Node Clicks

Adding A List View To A Form

Adding Items To A List View

Adding Icons To List View Items

Adding Small Icons To List View Items

Selecting The View Type In List Views

Adding Column Headers To A List View

Adding Column Fields To A List View

Handling List View Item Clicks

Handling List View Column Header Clicks

Adding A Tab Strip To A Form

Inserting Tabs Into A Tab Strip Control

Setting Tab

Setting Tab Images

Using A Tab Strip To Display Other Controls

Handling Tab Clicks

In Depth

In this chapter, we’re going to take a look at image list controls and some of the controls that use imagelists: tree views, list views, and tab strips These controls are part of the Windows common controlspackage and are being used more and more frequently in Windows programs

http://24.19.55.56:8080/temp/ch16\499-504.html (1 of 4) [3/14/2001 1:51:44 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 39

We’ll get an overview of each control before tackling the programming issues You add all the controls

in this chapter to the Visual Basic toolbox by selecting the Project|Components menu item, clicking theControls tab in the dialog box that opens, selecting the entry marked Windows Common Controls, andclicking on OK to close the Components dialog box

To use the images in the image list, you usually associate the image list with a Windows common

control (which has an ImageList property) For each item in the common control, such as a tab in a tab strip control, you can then specify either an index into the image lists’ ListImages collection or an

image’s key value to associate that image with the item

You can also reach the images in an image list with the ListImages collection’s Picture property For

example, if you wanted to use an image list with a control that’s not a Windows common control, such

as a picture box, you can assign the first image in the image control to that picture box this way:

Picture1.Picture = ImageList1.ListImages(1).Picture

The Image List Control tool appears in the Visual Basic toolbox in Figure 16.1 at bottom, on the right

Figure 16.1 The Image List Control tool.

Tree Views

If you’ve used the Windows Explorer, you’re familiar with tree views Tree views present data in ahierarchical way, such as the view of directories that appears in the tree view at left in the WindowsExplorer, as shown in Figure 16.2

Figure 16.2 The Windows Explorer.

Trees are composed of cascading branches of nodes , and each node usually consists of an image (set

with the Image property) and a label (set with the Text property) Images for the nodes are supplied by

an image list control associated with the tree view control

A node can be expanded or collapsed, depending on whether or not the node has child nodes At the

topmost level are root nodes, and each root node can have any number of child nodes Each node in a

tree is actually a programmable Node object, which belongs to the Nodes collection As with other collections, each member of the collection has a unique Index and Key property that allows you to

http://24.19.55.56:8080/temp/ch16\499-504.html (2 of 4) [3/14/2001 1:51:44 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 40

access the properties of the node.

The Tree View Control tool is the thirteenth tool down on the right in Figure 16.3

Figure 16.3 The Tree View Control tool.

List Views

The list view control displays, as its name implies, lists of items You can see a list view at right in theWindows Explorer in Figure 16.2 There, the list view is displaying a list of files Each item in a listview control is itself a ListItem object and can have both text and an image associated with it The

ListItem objects are stored in the list view’s ListItems collection.

List views can display data in four different view modes:

• Icon mode—Can be manipulated with the mouse, allowing the user to drag and drop and

rearrange objects

• SmallIcon mode—Allows more ListItem objects to be viewed Like the Icon view mode,

objects can be rearranged by the user

• List mode—Presents a sorted view of the ListItem objects.

• Report mode—Presents a sorted view, with sub-items, allowing extra information to be

displayed

The list view in the Windows Explorer in Figure 16.2 is displaying files in Report view mode (which isthe only mode that has columns and column headers) In this mode, you add sub-items to each item,and the text in those sub-items will appear under the various column headings

You usually associate two image list controls with a list view: one to hold the icons for the Icon viewmode, and one to hold small icons for the other three modes The size of the icons you use is

determined by the image list control (the available sizes are 16 × 16, 32 × 32, 48 × 48, and Custom).The List View Control tool is the fourteenth control down on the left in Figure 16.4

Figure 16.4 The List View Control tool.

Tab Strips

A tab strip control presents the user with a row (or rows) of tabs that acts like the dividers in a notebook

or the labels on a group of file folders Like an increasing number of other controls (such as coolbarsand tree views), tab strips represent one of Microsoft’s attempts to compact data into less and less ofthe screen (because there’s getting to be more and more data) Using tab strips, the user can click a taband see a whole new panel of data, like opening a file folder In fact, we’ve already used tab strips inmany parts of this book already to set Visual Basic options or to include ActiveX controls in our

http://24.19.55.56:8080/temp/ch16\499-504.html (3 of 4) [3/14/2001 1:51:44 AM]

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Ngày đăng: 14/08/2014, 01:20

TỪ KHÓA LIÊN QUAN