More Interesting Facts About VB This month's column continues with a look at some of the different and interesting aspects of the Visual Basic language, all presented together under the general title of "Did You Know?" An extra window of code Sometimes when you're writing a program, it would be handy if you could look at a couple of subroutines or pieces of code simultaneously. With VB 5 it's easy. All that's required is to open the code window as normal, and then move the mouse cursor to above the up arrow of the vertical scroll bar on the right hand side of the screen. The cursor will change in appearance to resemble two small parallel bars with arrows above and below. Next, click and hold down the left mouse button which will cause a thickened line to appear at the top of the code window. Continue to hold down the mouse button and move the cursor (the thickened line will also move automatically) to the position in the code window where you want the split to occur. Then release the mouse button. To alter the contents of one of the panes, move the cursor into it and click the left mouse button. This pane will now have the focus. Click the down arrow of the Procedure drop down list box (situated at the top of the code window--right hand side) and pick the desired subroutine. Now you can either view or edit the code. You can even display the contents of the same subroutine in the two panes simultaneously. This is handy when you need to edit a subroutine which contains lots of lines of code. So, for example, you could use the upper pane to look at any variables declared at the top of the subroutine, while you use the lower window to alter any statements. To revert back to having just a single code window, simply double click the thickened line separating the two panes. Alternatively, drag the thickened line to either the top or the bottom of the code window to make it disappear. A simple animation Here's an extremely simple animation which can be used in your applications to let users know something is happening. To set it up, create a standard VB application in the normal way. Then add a PictureBox control and a Timer control to the project's default form. Point the Picture property of the PictureBox control to any picture you want--it doesn't matter which one you choose. Now set the Enabled property of the Timer control to False, and its Interval property to 500. Next, add the following single line of code to the Click event of the default form: Private Sub Form_Click () Timer1.Enabled = True End Sub Then add the following code to the Timer control's Timer event: Private Sub Timer1_Timer () If Picture1.BorderStyle = 1 Then Picture1.BorderStyle = 0 Else Picture1.BorderStyle = 1 End If End Sub That's all you have to do. Run the program and observe what happens when you click the form. The PictureBox control starts pulsating, almost like a heart beating. This is the result of making the PictureBox's border continuously disappear and re-appear by setting its BorderStyle property to 0 and 1 respectively. With the Picture1.BorderStyle set to 0, there is no border, while the line Picture1.BorderStyle = 1 instructs VB to give the PictureBox a fixed single, and visible, border. Further experiment by setting the Timer's Interval property to higher and lower numbers to see what effect it has. So by writing a minimal amount of code, and setting just a few properties, you've been able to introduce a useful animated graphic into your application. Upper or lower case There'll be occasions when you'll want to dictate the case of the text a user types into a TextBox, for example, to programmatically convert a string like "John" into "JOHN". To see how easily this can be done, start a standard VB project, and add a TextBox control to the default form. Then add the following code to the KeyPress event of the TextBox control: Private Sub Text1_KeyPress(KeyAscii As_ Integer) Dim keyboardChar as String keyboardChar = Chr(KeyAscii) KeyAscii = _ Asc(UCase(keyboardChar)) End Sub Run the program, and type some text into the TextBox. Any lowercase text is immediately converted to uppercase. Let's take a look at how this code works. When a key is pressed on the keyboard, its ASCII value is passed in the variable KeyAscii to the Textbox's KeyPress event. ASCII, which stands for American Standard Code for Information Interchange, is a universally accepted way of representing textual data in binary format. These binary numbers can then be understood and processed internally by computers. For example, lowercase "a" is represented by 01100001 (in decimal terms this is the number 97) while uppercase "A" is 01000001 and 65 respectively. The Chr function is used to return the character associated with the ASCII value for whichever key has been pressed on the keyboard. This character is then stored in the variable "keyboardChar" (previously declared as a String variable). The next step is to use Visual Basic's UCase function to convert the character now stored in "keyboardChar" to its uppercase equivalent, so for example, "a" would become "A", "b" would become "B", and so on. The last step is to then convert this uppercase character to its ASCII equivalent, and that's achieved by using Visual Basic's Asc function. Alternatively, if there was a need to force any text entered by a user into a lowercase format, simply replace the UCase with the LCase function in the above segment of code. Reprinted from the August 1998 issue of PC Update, the magazine of Melbourne PC User Group, Australia |