If you need a progress bar in your VB6 application, but don’t want to add the reference to Microsoft Windows Common Controls, here’s a simple way to create a progress bar that doesn’t require any extra components.
Author’s Note: The sample program does use the slider bar from the Common Controls, but only for demonstration purposes—the slider control isn’t required to implement the solution. |
The trick is to use a VB6 line control, and then vary the length of the line to simulate a progress bar. You can get fancy with the bar’s color or width. With some minor code changes, you can easily create a vertical rather than horizontal progress bar.
To set up the demo, go into VB and create a new project. For demonstration purposes only, add a reference to the Microsoft Windows Common Controls, and add a slider bar to the bottom of the form. Resize the slider bar so it fits across the form.
Paste the following code into the default form:
Option ExplicitDim RightMost As LongDim Line1 As ControlPrivate Sub Form_Load() 'This section adds a line control to the form. Set Line1 = Form1.Controls.Add("VB.Line", "Line1", Form1) Line1.X1 = 240 Line1.X2 = 4080 Line1.Y1 = 1080 Line1.Y2 = 1080 Line1.Visible = True 'Save the rightmost point. RightMost = Line1.X2 Line1.BorderWidth = 4 'The fun part is here. Slider1.Max = 100 Slider1.Min = 1End SubPrivate Sub Slider1_Change() Call NewBar(Slider1.Value)End SubPrivate Sub NewBar(XPercent) Line1.X2 = (RightMost - Line1.X1) * (XPercent / 100) If Line1.X2 < Line1.X1 Then Line1.X2 = Line1.X1End Sub
Now run the program. As you slide the indicator across the screen, you'll see the line change to match the indicator.
Author's Note: If you add the line control to the form at design time, you can remove the Form_Load code down to the "Save the rightmost point" comment. |