A subscriber reported a bug in the Gauge Class issue. It turns out if you use the class several times in a row it does not paint the control correctly. The code in this issue fixes that problem. The entire issue appears below in its corrected form.
Thanks to Nanette Walsh for pointing this out to us.
Thank you for trying Code of the Week for Visual Basic.
Your free trial expires after you receive your fourth issue. If you want to continue to receive Code of the Week you can get 52 issues of COTW for only $19.95. This is a full year of Visual Basic source code and information to help with all your development. So don't wait, subscribe now! The quickest way to subscribe is to jump to our online order form at http://www.codeoftheweek.com/order.html
We accept payment by Mastercard, Visa, check or money order. All payments must be in U.S. Dollars. If you are an international customer paying by credit card is usually the easiest method of payment.
If you prefer not to visit our web site:
1. Enclose a note with your email address and name.
2. If you are paying by credit card, make sure you include the credit card number, credit card expiration and name on the credit card and your signature. If you are paying by check, make it payable to C&D Programming Corp.
3. Mail it to:
C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
4. As soon as we receive your payment you will be added to our paid subscriber list.
The source code in this issue is designed for Visual Basic 4.0 and higher. Similiar techniques can be used for Visual Basic 3.0, it would just need to be converted to a regular BAS module. If anyone is interested in VB 3.0 source code, please contact us. We don't currently have it available, but if we get enough requests we will convert it.
In this issue we are going to implement a gauge control without requiring the gauge control VBX or OCX. This will avoid having to ship yet another control with your application. It also provides some better error checking and some features to improve performance.
This class is a replacement for the standard Gauge VBX/OCX included in Visual Basic. It allows you to show a gauge or meter bar to indicate the progress of a particular task. The uniqueness of this class is that it uses a standard Picture Control.
All you do to use this class is drop a Picture Control on your form and resize it to the size you want. Create an instance of the class, define a couple of properties and execute your task. During your task you need to call the Current property to assign the current value to the gauge. Assigning this property will automatically update the control to show how close to completion the task is.
This class works by using the Line method to draw a box on the picture control to simulate a gauge. The Refresh method is the one that does all the work. A good exercise would be to modify it to work vertically as well as horizontally (which is the default method). If anyone implements this and would like to share it with the rest of our subscribers, let us know.
NOTE:
This UpdateInterval feature is really only important for when you have many steps (over 50 or so). Every screen refresh of the gauge takes a significant amount of CPU cycles (time). The more you refresh the control, the greater the overhead the gauge. Depending on the task, you can implement a gauge that will take more time to update than the entire process takes to run.
An example of a case like this would be a calculation loop that was able to be done completely in memory. If each iteration of the loop occurs very fast and you update the gauge on each iteration, it is likely that using the gauge will increase the total time to complete the calculation significantly. The UpdateInterval forces the gauge to only update every x times the Current property is incremented. Let's say you have a task that has 1000 steps. By default the UpdateInterval will be set to 10. This means the gauge will only update when Current is equal to 10, 20, 30, 40, and so on. Instead of updating the gauge 1000 times the cGauge class will only update it 100 times, which will save LOTS of CPU cycles.
This sample assumes you have a form that has a picture control on it called pictGauge.
Dim oGauge as New cGauge oGauge.QuickStart pictGauge, 0, 100 For x = 0 to 100 ... Do something ... oGauge.Current = x Next
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0004b.html
That concludes this issue of COTW. We hope you find the source code useful in your development.
The below describes the ways you can supply us some feedback about COTW. We would like to see our members help mold COTW into the best Visual Basic source code resource available. But to do that we need your feedback about what you like and what you do not like about COTW.
If you have any suggestions for topics you would like to see covered or questions about this issue, please email them to info@codeoftheweek.com or use online feedback form at http://www.codeoftheweek.com/feedback.html.
If you have any source code you would like to submit for possible inclusion in COTW, please fill out our online submission form at http://www.codeoftheweek.com/submission.html.
If you have received this issue in a free offer, please check out http://www.codeoftheweek.com to order your own subscription to COTW.
For a limited time you can get 52 issues of COTW for only $19.95. This is a full year of Visual Basic source code and information to help with all your development. So don't wait, subscribe now!