Hello World Program
STEP 2: ATTACHING THE CODE TO THE OBJECTS
Visual Basic is an event driven programming language. This means that the code is executed as a response to an event such as a mouse click. Next, you will write the appropriate code and attach it to the object and event.
1. Double click the Exit button. The code for the button will appear. It looks like this:
Private
Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.ClickEnd Sub
The first line starts the procedure, the second line marks the end of the
procedure. Any code that is written goes between these two lines.
2. Type End into the procedure. It should look like this:
Private
Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.ClickSave again by choosing Save All.
| 3. Run the program. Click on the Start Debugging button on the toolbar. Click on the Hello and Clear buttons. Nothing happens because the code has not yet been written for these buttons. Click on the Exit button and the program closes. | ![]() |
| 4. Next, you'll make the computer beep when you click the
Exit button. Double click the Exit button, and add the Beep ()
statement above the End statement. It should look like the picture to
the right. 5. Run the program and see if the beep works when you click the Exit button. You'll only hear the beep if you use headphones or speakers. Save again by choosing Save All. |
![]() |
| 6. Attach code for the Hello button. Double click the
Hello button and add the following code: txtDisplay.Text = "Hello World!" To assign a new value to a
property from within the program's code follow this convention: |
![]() |
| 7. Attach code to the Clear button. Double click the Clear
button and insert the following code: txtDisplay.Text = "" 8. Run the program. See if the buttons work properly. Save again by choosing Save All. |
![]() |