Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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:
 
Previous Table of Contents Next


Adding Status Bars To Forms

You’ve finished your program, and it’s ready to go to market—but suddenly the project director calls and asks why there’s so many message boxes popping up all the time. You explain that you have to give the user feedback on the file downloading process—after all, downloading the 200MB initialization file from the Internet takes some time, and you like to update the user on the process every time a kilobyte of data has been read.

“What about using the status bar?” the project director asks.

Hmm, you think—what about using the status bar?

The easiest way to put a status bar in a form is to design your program with the Application Wizard, and the result of that process appears earlier in Figure 4.2. However, you can also add status bars to a program yourself with these steps:

1.  Use the Project[vbar]Components item to open the Components box, and select the Controls tab.
2.  Click the Microsoft Windows Common Controls box, and click on OK to close the Components box.
3.  Double-click the New Status Bar tool in the toolbox to add a new status bar to your form now.
4.  Right-click the status bar, and select the Properties item in the pop-up menu that appears, opening the button’s property page, as shown in Figure 4.8.


Figure 4.8  Adding panels to a status bar.

5.  Status bars are organized into panels, and each panel can display separate text. To add the panels you want to the status bar, use the Insert Panel button. Close the property page.
6.  Now you can set the text in the panels from code. You do that with the status bar’s Panels collection. The first panel in the status bar is Panels(1), the second Panels(2), and so on. For example, to set the text in the first panel to “Status: OK”, you would use this code:
Private Sub Command1_Click ()
    StatusBar1.Panels(1).Text = "Status: OK"
End Sub

The result appears in Figure 4.9—now we’re using status bars in our programs.


Figure 4.9  A new status bar in a program.

Referring To The Current Form

You’ve written a terrific subroutine to change a form’s color to red

Sub ColorWindow(FormToColor As Form)
    FormToColor.BackColor = RGB(255, 0, 0)
End Sub

and you want to color all the forms in your project when the user clicks a button. That’s easy to do using the Me keyword, which refers to the current object. Here, for example, is how we’d pass the current form to the ColorWindow() subroutine:

Private Sub Command1_Click()
    ColorWindow Me
End Sub

That is, Me is an implicit variable, always available, and stands for the current object, which comes in handy when you want to pass the current object to a procedure.


TIP:  The Me keyword is also very useful in class modules where more than one instance of a class can occur, because it always refers to the current instance.

Redrawing Form Contents

You’ve written some code to draw an “x” across a form like this:

Private Sub Command1_Click()
    Line (0, 0)-(ScaleWidth, ScaleHeight)
    Line (0, ScaleHeight)-(ScaleWidth, 0)
End Sub

You try it out and it looks perfect—but then the boss walks past and you minimize your program for a second to go back to that word-processing program so you’ll look busy. When you maximize the x program again, the x is gone—what happened?

One of the biggest headaches for Windows programmers is refreshing the window when required, because that involves redrawing the entire form’s contents. To make matters worse, this is a common occurrence, because in Windows, the user is always covering and uncovering windows, minimizing and maximizing them, and changing their size, all of which means that your program has to keep redrawing itself.

In C or C++ programs, you have to write all the redrawing code yourself; fortunately, there is an easy fix in Visual Basic (and that’s one of the things that made Visual Basic so popular in the first place)—you just use the AutoReDraw property. You’ve probably already used the AutoReDraw property, but we include it here for reference. When you set this property to True, as shown in Figure 4.10, the graphics displayed in the form are stored and redisplayed when needed. All the window refreshes are done for you.


Figure 4.10  Setting AutoReDraw to True.

Now when you minimize and then maximize your x program, the x reappears as it should. Problem solved!

Setting Control Tab Order

Another call from the Testing Department. They’ve been going over your program with a fine-tooth comb and asking about the keyboard interface.

What does that mean? you ask.

They explain that theoretically, according to Microsoft, users should be able to run all Windows programs with the keyboard alone.

But that was archaic years ago, you say.

Add it to your program, they say.

In Visual Basic, you can make controls accessible to the keyboard by setting their tab order. The user can move around from control to control, highlighting the currently selected control, using the Tab key. But it’s up to you to set the order in which the focus moves from control to control, and even whether or not a control can be reached with the Tab key.

To set the tab order of the controls in your program, follow these steps:

1.  Select a control whose tab order you want to set with the mouse, as shown in Figure 4.11.


Figure 4.11  Setting a control’s TabIndex property to set its tab order.

2.  Next, make sure the control’s TabStop property is set to True, as shown in Figure 4.11. If this property is False, the user cannot reach the control using the Tab key.
3.  Now set the control’s position in the tab order by setting its TabIndex property. The first control in the tab order has a TabIndex of 0, the next a TabIndex of 1, and so on.
4.  When you run the program, the first control is highlighted; when the user presses the Tab key, the focus moves to the second control in the tab order, when he presses Tab again, the focus moves to the third control, and so on.

That’s all it takes—now you’re giving your program a keyboard interface.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.