Written By TheVBProgramer.
To create a basic "Hello World" program, perform the following steps.
· Set the following properties of the controls:
Control Property Value
Command2 (Name) cmdSayHello
Caption Say Hello
Label1 (Name) lblHello
Caption (clear the caption by deleting the default "Label1")
Command1 (Name) cmdExit
· Expand the width of the label control by making it about twice its original size. The controls should now look like this:
· Click the label control once to select it, then press F4 to bring up the Properties window. Select the Font property. A button with an ellipsis (…) should appear, as shown below:
· Click the ellipsis button, and a Font dialog box appears. Select the Comic Sans MS font (if available, otherwise, choose something else), Italic style, size 12.
When you are done making your selections, click OK to dismiss the Font dialog box.
· Double-click the cmdExit button. This will cause the code window to open, with a stub for the event procedure related to the Click event of the cmdExit button pre-coded.
An event procedure header consists of :
Ø the keywords Private Sub
Ø the name of the procedure, which VB formulates by taking the name of the control (in this case cmdExit), followed by an underscore, followed by the name of the event (in this case Click)
Ø zero or more arguments enclosed in parentheses. If the event does not pass any arguments, the parentheses will be empty, as is the case here.
The end of a procedure is signified by a line containing the keywords End Sub.
It is between the procedure header the End Sub line that you place the code that you want to be executed when the event occurs. In other words, you are saying, "When the user clicks this button with the mouse, do this …".
· In the code window, type the word End between the procedure header and the End Sub line. (Style-wise, I prefer a blank line after the procedure header and before the End Sub line, and I start all code indented one tab-stop, or four spaces in.) Your code window should look like this:
Note that the top of the code window contains two drop-down boxes. The drop-down box on the left (the object box) contains entries for all of the controls on your form, as well as an entry for the Form itself and an entry called "(General)".
The drop-down box on the right (the procedure box) will show all of the possible events that the control selected in the object box can respond to. So with "cmdExit" selected in the object box, click the down arrow on the procedure box, and you will see all of the events that a command button can respond to.
You only need to write code for the events you want your program to respond to. So even though the command button can respond to around 17 different events, we are only interested in one here: the Click event.
Recall that when you opened the code window by double-clicking on the command button, the Click event was selected by default. For each control, VB takes a guess as to which event you most likely want to write code for. If VB guesses wrong, simply go to the procedure box and select the event that you want. (You can also delete the "Private Sub" and "End Sub" lines for the event procedure stub that you don't want.)
· Double-click the cmdSayHello button. This will cause the code window to open with the stub for the cmdSayHello_Click event procedure. In this event, we want to code lblHello.Caption = "Hello World", so that when the user clicks the "Say Hello" button, the text "Hello World" will display in the label. Start typing the line of code with lblhello.c. Notice that as soon as you typed the dot, a listbox appears which contains all of the properties and methods that can be used with a label. This is VB's "Intellisense" feature at work, ready to assist you with your coding. When this box appears, you have the following options:
Ø Ignore it and keep typing
Ø If the item you want is selected, press the Tab key, which will cause that word to appear on your coding line and dismiss the box
Ø If the item you want is not selected, type a few more letters of the word until it is selected, then press Tab to complete it
Ø If the item you want is not selected, scroll down to find the item in the box, click it, and then press Tab to complete it
Ø Note: If you press Enter instead of Tab when the word is selected, VB will complete it and move to the next line (you would only want to do this if the desired item is the last item in the coding statement).
However you type it in, the completed procedure should look like this:
Believe it or not, that's all the coding that is required for this sample program. At this point, we're ready to run.