link to hitmill.com home page

Hello World with Visual Basic 6

by Cheryl Gribble

This is a free online Visual Basic 6.0 Hello World tutorial for students new to coding with Microsoft's Visual Basic 6.0 programming language. Each step in using the IDE (Integrated Development Environment) to complete the Hello World project is explained in detail.

picture of the completed project
Clear and Exit are buttons named cmdClear and cmdExit. The white box is a text box named txtHello.
Purpose of the project: To display the text "Hello World!" in a text box control, to be able to clear the text from the text box using a command button, and to exit the project using a command button. Open Visual Basic 6.0. Open a Standard .exe project. Maximize the white-background project form by clicking on the middle maximize button to the right of the Project1 title bar.
Directions follow for naming the Project:
To name the project, double-click in the project window on the Project1 icon. Look into the Properties Box where it now says (Name) Project1 and double-click on (Name). This should now highlight the text, Project1. If Project1 is highlighted just type HelloWorld (no spaces). Press the Enter key. The project is now named HelloWorld.
You could similarly highlight the form in the project window, double-click the form icon, and find the name property for the Form1 in the Properties Window, then rename it, if you so desire. I did not rename the Form1 for this Hello World demonstration. To place a text box onto your form find the text box control inside the tool box to the left of your work area. Double-click on the text box to put a text box onto your form. Use the resizing handles to size it the way you wish and drag the control using the left mouse button to move it where you would like to have it place on your form. Select the text box so that selection handles (resizing handles) appear around the text box control. In the properties window scroll to the text property and where the text reads Text1, highlight Text1 using your mouse, and delete it using the delete key. Press Enter. Now the text box control on the form should appear to be empty. With the Text1 object still highlighted, double click on the name property so the I-beam of the cursor is inside the box where you will name the text box object. Type txtHello and press the Enter key.
To place the 3 command buttons onto your form, find the command button control object in the tool box and double-click on the command button 3 times to place three buttons onto your form. Use the left mouse in the down position to drag each command button into place. Select the first command button by clicking it with the mouse one time. Selection handles (called resizing handles) appear around the command button. When an object is selected the properties for that particular object appear in the Properties window. Double-click on the name property and type a name: cmdHello. Scroll down to the caption property and double-click caption: Type &Display Hello. The ampersand is located above the number 7 on the keyboard. Press the Enter Key.
Note: When the ampersand (&) is used before a letter in a caption, the letter following the ampersand is underlined. Any underlined letter in a caption may be used in combination with the ALT key + the underlined letter as a keyboard short-cut to access that control. A user may press ALT + D instead of clicking with the mouse on the first command button, the one with the caption Display Hello.
Now name the second command button:
Select the second command button so that you see the resizing handles. In the Properties window, double-click on (Name) and name this button cmdClear. Double-click on the Caption property in the properties window and give it a Caption of Clear. (The C should be underlined in the caption, for this project.) Press the Enter key.
(NOTE: Sometimes you will want to underline a different letter in other projects. Place the ampersand before the letter you wish to underline. Do not have the same letter underlined more than once in a project window. Two different captions should not each have an underlined D. Pick a different letter to underline when this happens. Select a letter that has not yet been used to underline in a caption).
Name the third command button:
Select the third command button so that you see the resizing handles around the third command button. In the Properties window, double-click on the (Name) property and name it cmdExit. Make the Caption property E&xit with the ampersand before the x so that the x will be underlined. Press the Enter key. Now you have named and labeled the Exit key.
SUMMARY: You have opened a standard .exe project and named the project HelloWorld. The form was not renamed by me but is still called Form1. Rename the form if you wish. You have placed one text box and 3 command buttons onto your form and you have named the controls and given the command buttons separate captions. (A caption on a button is what the user sees on that button.) Text boxes do not have a caption. Use the .text property for text boxes. You have deleted default text from the text box control to make it appear empty, and have named this control, txtHello. Now you will code the command button controls.
To access the code window with a sub procedure already started for you double-click on the cmdHello button with the caption that reads Display Hello. This double-click action takes you to the code window where you see a line reading Private Sub cmdHello_Click() and another line reading End Sub. Place your cursor between these two lines and indent once using the tab key. Type txtHello.Text="Hello World!". Use the quotes this time when typing that line. Press the Enter key. On the next line indent once using the tab key and type With txtHello, then press the Enter key. On the next line indent twice using the tab key and type .font="Arial" using the period before font and quotes around Arial. Press the Enter key. After the .font="Arial" line, on the line below indent twice using the tab key and type .FontSize=16 (no quotes but must have the period before .FontSize. Press the Enter key. On the next line indent twice using the tab key and type .ForeColor=vbBlue (no quotes, just the period before ForeColor. (You are almost through with this control). You will only indent once on the next line, using the tab key. Type End With and press the Enter key. Allow the last line of text for this sub procedure to read End Sub (no period). You have now coded the entire cmdHello sub procedure which will place text into the txtHello control when the program runs, will change the text to a 16 point Arial font and the text color (ForeColor) will be blue.
From the Visual Basic menu bar, click on View and select Object to get back to the form. You will code the sub procedure now for the second command button, the cmdClear button with the caption of Clear:
Double-click on the cmdClear button on the form. In the code window and following the line of text that reads Private Sub cmdClear_Click(), indent once using the tab key. Type txtHello.Text="", that is two quotes without a space or any content between them. This represents an empty string which appears as an empty text box when the cmdClear button is clicked or accessed from the keyboard (Alt + C). You are already through coding the cmdClear event. Let the last line of this event to be the End Sub. Go back to View, Object to return to the form.
You will code the cmdExit procedure. Double-click on the cmdExit button. In the event procedure beginning Private Sub cmdExit_Click, drop down one line, indent, and type End (no period). That's it! Return to the form (View, Object) and run your procedure (F5 button). The total code for your project looks like this:
Private Sub cmdClear_Click()
    txtHello.Text = ""
End Sub
Private Sub cmdExit_Click()
End Sub
Private Sub cmdHello_Click()
    txtHello.Text = "Hello World!"
    With txtHello
        .Font = "Arial"
        .FontSize = 16
        .ForeColor = vbBlue
    End With
End Sub

Visual Basic is not a command line program so the order of the coded events does not matter in your code. No event will be triggered unless the user accesses a command button either by clicking on it or using your coded short-cut key with the Alt key. The only thing that makes a click event work is by the user "clicking" on something (or using the coded short-cut). If the user clicks on a command button and no code is written, nothing will happen either. A user event is a mouse click, a key press, dragging the mouse over a certain area, and so forth. When a user event occurs and that object has a sub procedure coded for that user event, then the code will run. The programmer only programs what will happen "IF" a user event occurs.

The programmer cannot control the user. If I, the user, do not drag my mouse cursor over some magic area that is programmed, then nothing will happen even though the programmer went to all that trouble to write the code for a "mouse over" event. This is what is meant by the term "event-driven programming language". (In a command line program the code would run in sequence, one line after another, regardless of any user events.)
If this tutorial has been helpful, please let us know... e-mail contact is below.



Contact Webmaster

© hitmill.com
All Rights Reserved