Step 4: Add Visual Basic Code
In the previous lesson, you used the Properties window to configure the properties of the controls on a form. In this lesson, you will add the code that will control your program's functions.
For a video version of this topic, see Video How to: Creating Your First Visual Basic Program.
To add code and functionality to your program
In the Form Designer, double-click the Button control.
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 pointer is automatically located inside the Button's procedure—you can just start to type code.
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 every time that the button is clicked. You may also have noticed that the pointer was located inside the event procedure, so all that you had to do was type the code.
The code that you typed (WebBrowser1.Navigate(TextBox1.Text)) tells the program to take the text that was typed into TextBox1 and pass it as an argument to the Navigate method of the WebBrowser control (named WebBrowser1). To learn more about properties, methods, and events, see Closer Look: Understanding Properties, Methods, and Events
If you don't understand the code, don't worry—you will learn much more about how to write code in following lessons.
Your application is now complete! In the next lesson, you will run your first Visual Basic application.
Next Lesson: Step 5: Run and Test Your Program