|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
You can also set a status bar panels image at runtime. For example, heres how we set the image in the first 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
Handling Panel ClicksAre 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 arguments Index or Key properties. For example, heres 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 youve 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
Case "Date"
Panel.Text = Date$
Case "Time"
Panel.Text = Time$
End Select
End Sub
Adding New Panels To A Status Bar At RuntimeIts easy to add a new status bar panel at runtimejust use the Panels collections Add method. Heres 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
Creating Simple Status BarsTheres 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. Heres an example; in this case, we just display the message Status: OK in the simple status bar when the user clicks a button:
Private Sub Command1_Click()
StatusBar1.SimpleText = "Status: OK"
End Sub
The result of this code appears in Figure 15.24.
Adding A Progress Bar To A FormThe Testing Department is calling again. Why does downloading the 200MB data file your program requires take so long? Well, you explain, the Internet is like that. They ask, but cant you at least show the 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 controls display a colored band that can grow (or shrink) as time goes on. To add a progress bar to a form, follow these steps:
Now youve got a new progress bar in your formbut how do you use it? See the next topic. Using A Progress BarNow that youve 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 bars 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. Lets see an example. In this case, well 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 timers Interval property to 1000 (in other words, 1000 milliseconds, or 1 second). Well leave the progress bars Min property at 0 and its Max property at 100, the defaults. When the form loads, we disable the timer and set the progress bars 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 might want 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 bars Value property every second. We also check if weve filled the progress bar, and if so, disable the timer:
Private Sub Timer1_Timer()
ProgressBar1.Value = ProgressBar1.Value + 10
If ProgressBar1.Value >= 100 Then Timer1.Enabled = False
End Sub
Thats all we neednow when the user clicks the command button, we start the progress bar in motion, and it goes from 0 to 100 in 10 seconds, as shown in Figure 15.25.
|
|
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. |