This column provides techniques, tips, and tricks to help you gain familiarity and experience with Microsoft Visual Basic: a popular language that enables you to quickly write Windows applications. How to make an image control appear depressed There are occasions in your applications when you want to associate some functionality with a user clicking on an image. This is easy to do - just include the required code into the click event of the image control. However, your application would look more professional if the image control appeared "depressed" when the user clicked on it. To do so, add the following code to your application. Note that at design time the BorderStyle property of the image control should be set to 0 (indicating no border). In the MouseDown event of the image control, add: Image1.BorderStyle = 1 ' The value 1 specifies a ' Fixed Single border In the MouseUp event of the image control, add: Image1.BorderStyle = 0 ' The value 0 specifies that ' there should be no border Shortcut for declaring static variables Consider the following code in the click event of a command button named Command1: Private Sub Command1_Click () Dim i as Integer Dim j as Integer i = i + 1 Print i j = j + 2 Print j End Sub Repeated clicking of the command button will cause the following series of numbers to be printed on the active form: 1,2,1,2,1,2........ To get this event procedure to "remember" the values of i and j between successive clicks of the command button, just alter their declaration to be as follows: Static i as Integer Static j as Integer Repeated clicking of the command button will now display the series 1,2,2,4,3,6,4,8... However, a quick trick if you want all of the variables within a procedure to "remember" their values is to alter only the subroutine header definition as follows (there is no need to change the individual variable declarations from their original format): Private Static Sub Command1_Click () Dim i as Integer Dim j as Integer Finding fonts common to the screen and the printer Sometimes in an application there is a requirement to use the same font to show a user information on both the screen and in hardcopy format. To add a professional touch to your program, you should then only use those fonts that are available on both your user's display device and printer. The following code shows how to compare the fonts for the display device and the printer to produce a list of fonts that are common to both. Start a new project and add 3 list boxes to the form. The first list box will contain the fonts for the display device, the second list box will hold the fonts for the printer, and the last list box will hold the common fonts. Private Sub Command1_Click() Dim i As Integer Dim j As Integer MousePointer = vbHourglass 'Screen Fonts Coding List1.Clear For i = 0 To Screen.FontCount - 1 List1.AddItem Screen.Fonts(i) Next i 'Printer Fonts Coding List2.Clear For j = 0 To Printer.FontCount - 1 List2.AddItem Printer.Fonts(j) Next j 'Both Screen & Printer Fonts Coding List3.Clear For i = 0 To Screen.FontCount - 1 For j = 0 To Printer.FontCount - 1 If Screen.Fonts(i) = Printer.Fonts(j) Then List3.AddItem Screen.Fonts(i) End If Next j Next i MousePointer = vbDefault End Sub Comments regarding the above code Notice the use of the predefined MousePointer constants "vbHourglass" and "vbDefault", in conjunction with the MousePointer property, to indicate that a lengthy operation is underway. (vbHourglass is used to turn the mouse pointer into an hourglass icon whilst the list boxes are being populated.) The code in the "Screen Fonts Coding" section adds the screen fonts to the first list box. It achieves this through the use of the FontCount and Fonts properties. These properties are applicable to both the Printer and Screen objects in VB. The FontCount property returns the number of fonts available, whilst the Fonts property holds the font names. Similarly, the code in the "Printer Fonts Coding" section adds the printer fonts to the second list box The final section of code, "Both Screen & Printer Fonts Coding" uses two "For" loops to detect the common fonts which are then added to the third list box. Finally, the mouse pointer is returned to its default value to indicate to the user that the operation is finished. Instead of populating a list box as in this example, another alternative would be to load the common fonts onto a menu. This could be coded as a standard menu, or as a popup menu which appears when the user "right clicks" the mouse button in an appropriate location in the application. Note: the output in the list boxes is easier to read if the "Sorted" property for each of them is set to "True" at design time. Multiple declarations on one line Be extremely careful if it is your preferred programming style to declare more than one variable on a single line of code. It can catch you out when using the VB programming language. For example, consider the following: Dim i, j, k as Integer Inexperienced VBers might think that all three variables have been successfully declared as integers. In fact, it is only the variable k that is an integer - i and j are Variant data types because their explicit type was omitted (Variant is the default type). You can verify this by putting the following code into the click event of a command button: i = "Bob" j= "Mary" k= 10 Run the program, and click the command button. All will be fine. Now change the last line to read k="Jill", and re-run the program. This time the program will abort with a Type mismatch run-time error. A book suitable for all Windows developers
If you want to know more about how to write new applications for Windows 95 (or port existing Windows 3.x ones to it) this book is full of helpful advice, tips, techniques and code to make the job easier. |