Darren Provine at Rowan University


(Click here for text version.)

Fun VB Stuff

Quick Input
If you want to have your form so you can enter lots of information in one text box and then hit a particular button, you should set the button's "Default" property to true, and put this code at the end of step 2 (and before step 3):
    ' set the selection to the entire text box
    txtInput.SelStart = 0
    txtInput.SelLength = Len(txtInput.Text)
    ' set the keyboard focus back to the text box
    txtInput.SetFocus
This will set the keyboard focus back to the text box, and select the entire thing so you can just type new information without having to delete the old stuff first.

I've made up a sample form to show how it works. (Note that the "erase" button also sends the focus back to the text box, using the same code.)

Picture Boxes That Remember
If you move another window in front of your VB form, anything displayed in a picture box gets lost. The reason is that the picture box doesn't know it's supposed to repaint itself. To tell your picture boxes that they're supposed to automatically repaint, set the "AutoRedraw" property to True.
Fancy Message Boxes
You can fancy up your message box by giving it more arguments. By default, if you give it one argument, it just shows the message:
    MsgBox("There was an error.")
But you can give it more arguments, changing how it looks:
    Call MsgBox("There was an error.", vbExclamation, "My Great Program")
The second argument specifies which icon to put on the box, and the third argument says what the title bar of the message box should be.

The "vbExclamation" can be replaced by other values; when you type in your program and hit the comma, you'll get a list. For Intro, you want to avoid most of them. The three safe ones are vbExclamation, vbCritical, and vbInformation.

NOTE: if you do this, you have to put "Call" in front of "MsgBox".


(US flag) This page's URI: http://elvis.rowan.edu/~kilroy/class/intro/?FunStuff
Last modified: Tuesday, 27 October 2009, 1:13:49pm