double-click the OK button. The code window will open with the following code. This is the
shell of the button’s Click event and is the place where you enter the code that you want to
run when you click 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 Button1.Click
End Sub
As a result of the typographic constraints in publishing, it is not possible to put the Sub
declaration on one line. Visual Basic 2008 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 2008 to do something special. In this case, it tells Visual Basic 2008
that this is a subroutine, a procedure that does not return a value. 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 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 to the user MessageBox.Show("Hello, " & txtName.Text & _ “! Welcome to Visual Basic 2008.”, _ “Hello User Message”)
End Sub
3. After you have added the preceding code, go back to the Design tab, and double-click the Exit
button. Add the following code to the btnExit_Click event procedure:
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
'End the program and close the form
Me.Close()
End Sub
You may be wondering what Me is. Me is a keyword that refers to the form. Just like the pronoun me, it is just shorthand for referring to one’s self.
4. Now that the code is finished, the moment of truth has arrived and you can see your creation.
First, however, save your work by using File >> Save All from the menu or by clicking the Save
All button on the toolbar. The Save Project dialog box is displayed as shown in Figure 18,
prompting you for a name and location for saving the project.
By default, a project is saved in a folder with the project name; in this case Hello User. Since
this is the only project in the solution, there is no need to create a separate folder for the
solution which contains the same name as the project, thus the Create directory for solution
check box has been unchecked.
5. Now click the Start button on the toolbar. You will notice a lot of activity in the Output
window at the bottom of your screen. Provided that you have not made any mistakes in entering
the code, this information just lets you know which files are being loaded to run your
application.
At this point Visual Studio 2008 will compile the code. Compiling is the activity of taking
the Visual Basic 2008 source code that you have written and translating it into a form that the
computer understands. After the compilation is complete, Visual Studio 2008 runs (also
known as executes) the program, and you’ll be able to see the results.
Any errors that Visual Basic 2008 encounters will be displayed as tasks in the Task List window.
Double-clicking a task transports you to the offending line of code. You will learn more about how
to debug the errors in your code in the site.
6. When the application loads, you see the main form. Enter a name and click OK or press the
Alt+O key combination (see Figure 19).
A window known as a message box appears as shown in Figure 20, welcoming the person
whose name was entered in the text box on the form — in this case Stephanie.
No comments:
Post a Comment