Visual Basic

Common Forms

How many times have you created an "About" form for an application? At the end of the day, all an "About" form does is provide version and copyright information for the application. Copyright information will be standard within an organization, and version information about the program is available via the App object. Therefore, "About" is a great example of a generic form that can query the application for the information it needs, and should require no application-specific code in it.

Want a "Tip of the Day" form? Again, this is a generic form that can get its information from a resource file (for the tips) and the Registry (for the last tip displayed and whether to show on startup).

OK, so you've got your error handling sorted out, but you'd like to make it a bit more intelligent than simply showing a message box with the standard Abort/Retry/Ignore buttons. Some errors are prime candidates for the application to retry periodically but still give the user the ability to abort. All you need is to create a form that looks like the message box form, but you should also add a timer. Then all the form needs is a few properties allowing the buttons to be hidden and shown at will, and a property to tell it to automatically retry the error action every x seconds. Using the Timer control, the error dialog box can click its own "Retry" button whenever the timer fires. Once you have all this code together, there's no reason why this can't be a generic form. Most people already use MsgBox to report errors and that's about as generic as forms come!

Speaking of MsgBox, have you ever needed an extra button, or a combination of buttons that's not permitted, or a button with a completely different name than the standard ones? Simply subclass the MsgBox function and have your own form that mimics the functionality of MsgBox. Now if you need an extra button, you simply need to define a new constant for it and its caption. All the new MsgBox code and forms can then become generic, as they are passed all the information they need from the application when the function is called.

Other useful features we have added to our MsgBox function include automatic logging of critical messages (where the button includes vbCritical) and appending the application title and version details to the beginning of the title text.

by BrainBellupdated
Advertisement: