Visual Basic 4.0, like any other programming language, has its share of tricks, techniques and trips that can save you both time and effort. Let's have a look at what's available. For and against the variant data type A variant data type can hold a variety of data types - numeric, date/time, or string values. However, such flexibility comes at a cost - variant data types require more memory and they are slower (because VB handles any necessary conversions). Populating a list at design time Instead of populating a list box at run time using the AddItem method, VB 4.0 allows you to add entries to a list at design time. After double-clicking the list box control in the toolbox (this puts a list box control in the centre of the form), press function key F4 to bring up the properties. Click on the "List" property, then the down arrow that appears, and start entering the list items. After each item, use the keyboard combination of Ctrl + Enter keys to position the insertion pointer ready for the next entry. If you just press the Enter key, the box displaying the list items closes. Experimenting with the Windows API The Windows API (Application Programming Interface) is a set of functions that are part of Windows which can be called from your VB application. It allows you to extend the power of VB. However, if you don't use it correctly, for example, by making a typing mistake, you can hang your application. So when developing your application, make sure that you set the "Save Before Run" option, with or without a prompt (to set it, select the menu options: "Tools", "Options", and the "Environment Tab"). Doing this will ensure that any code that you have entered will not be lost due to a catastrophe. The quick way to set a breakpoint A breakpoint is a location in your code where you want VB to pause your application so you can do some investigating. To set a breakpoint quickly, open the code window, find the line where you want VB to pause, and click it using the right mouse button. When the pop-up menu appears, click on the "Toggle Breakpoint" option. Repeating this process on a line already nominated as a breakpoint will remove the breakpoint. A cluttered form? To preserve screen real estate on a busy form (that is, one with lots of controls) use combo boxes with the "Style" property set to "Dropdown Combo" or "Dropdown List". If you use the latter option, your user can only select an entry from the list rather than enter a new one. Word processors and forms If you want to look at a textual description of a form and the controls it contains, you use a word processor such as Windows 95 "WordPad". When opening a form using WordPad, remember to change "Files of type" from its default setting of *.DOC to "All Documents", otherwise WordPad will not be able to locate the forms that have a suffix of .FRM. The reason a form can be viewed using a word processor is that a form is internally represented using ASCII (American Standard Code for Information Interchange). When you look at a form in this way, you will see the VB version number, all the properties of the form and the properties of any controls contained on the form, and all of the code associated with the form. The flexibility of using a word processor in this way provides you with an easy, flexible and attractive way of documenting your VB project. However, there is another way of printing the ASCII representation of a form without using a word processor. From the File menu in design mode, simply select the "Print" option. You are then presented with a form containing print options. So for example, you can choose to print the form's image, or a textual description of the form, or the code associated with the form (or all three). You also have the option of printing these to a file for later use. Unfortunately, there is now no excuse for having a poorly documented VB project! Carriage return and line feed constant To improve the look and readability of your message boxes, use the VB provided constant "vbCrLf" to insert carriage returns and line feeds at the appropriate places. For example:
Other useful predefined constants are:
To see the effectiveness of using a Load statement, set up a sample application containing two forms (Form1 and Form2) as follows.
For Form1, put the following code into the command button's click events:
Note the use of the VB defined constants "vbHourglass" and "vbDefault" for setting the mouse cursor to the familiar Windows hourglass and default shapes respectively.
This sample application employs a simple technique that can be used to noticeably alter the user's perception of your application.
|