Datasheet
P1: GIG
WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49
Welcome to Visual Basic .NET
or subroutines for the object selected in the left combo box. If this particular form had a lot of code behind
it, these pull-downs would make navigating to the desired area very quick—jumping to the selected area.
However, since all of the code fits in the window, there are not a lot of places to get lost.
Now look at the code in the window. The code in Visual Studio .NET is set up into regions designated by
the plus (+) and minus (–) buttons along the left side. These regions can be collapsed and expanded in
order to simplify what you are looking at. If you expand the region labeled Windows Form Designer
generated code, you will see a lot of code that Visual Basic .NET has automatically generated for you,
which takes care of defining each of the controls on the form and how the form itself should behave. You
do not have to worry about the code in this region, so collapse the Windows Form Designer generated
code region once more and concentrate on the code you have to write.
Try It Out Adding Code to the HelloUser Project
1.
To begin adding the necessary code, click the Design tab to show the form again. Then
double-click on the OK button. The code window will reopen with the following code. This is the
shell of button’s Click event and is the place where we enter the code that we want to run when
we click on the button. This code is known as an event handler and sometimes is also referred to
as an event procedure:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
End Sub
Due to the typographic constraints in publishing, it is not possible to put the Sub declaration on one line.
Visual Basic .NET allows you to break up lines of code by using the underscore character (
) to signify a
line continuation. The space before the underscore is required. Any whitespace preceding the code on the
following line is ignored.
Sub is an example of a keyword. In programming terms, a keyword is a special word that is used
to tell Visual Basic .NET to do something special. In this case, it tells Visual Basic .NET that this is
a procedure. Anything that you type between the lines Private Sub and End Sub will make
up the event procedure for the OK button.
2. Now add the highlighted code into the procedure:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
'Display a message box greeting the user
MessageBox.Show("Hello," & txtName.Text & _
"! Welcome to Visual Basic .NET.", _
"HelloUser Message")
End Sub
Throughout this book, you will be presented with a code that you should enter into your program if you are
following along. Usually, we will make it pretty obvious where you put the code, but as we go we will
explain anything that looks out of the ordinary.
3. After you have added the code, go back to the Design tab, and double-click on the Exit button.
Add the highlighted code to the btnExit
Click event procedure.
29