Step 4: Add Visual Basic Code
In the previous lesson, you used the Properties window to configure the properties of the controls on your form. In this lesson, you will add the code that will control your program's functions.
For a video demonstration, see Video: Creating Your First Visual Basic Program.
To add code and functionality to your program
In the Form Designer, double-click thecontrol.
A new window called the Code Editor opens. This is where you add all the code for your program.
In the Code Editor, type the following.
This code will run when users click the button.
When the Code Editor opens, the cursor is automatically located inside the Button's procedure - you can just start typing.
You may have noticed that when the Code Editor opened, it already contained some code that looked like this:
Private Sub Button1_Click(ByVal sender As System.Object...
This code is an event handler, also called a Sub procedure. Any code inside this procedure (between Sub and End Sub) runs each time the button is clicked. You may also have noticed that the cursor was located inside the event procedure, so all you had to do was type.
The code that you typed (WebBrowser1.Navigate(TextBox1.Text)) tells the program to use the Navigate method of the WebBrowser control (named WebBrowser1) with an argument of TextBox1.Text (the value contained in the Text property of the TextBox control). To learn more about properties, methods, and events see
If you don't understand the code, don't worry—you will learn a lot more about writing code in following lessons.
Your application is now complete! In the next lesson, you will run your first Visual Basic application.