Share via


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.

link to video 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

  1. 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.

  2. In the Code Editor, type the following.

    WebBrowser1.Navigate(Textbox1.Text)
    

    This code will run when users click the button.

    Tip

    When the Code Editor opens, the pointer is automatically located inside the Button's procedure—you can just start to type code.

Closer Look

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...

|

End Sub

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.

Next Steps

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

See Also

Tasks

Step 1: Create a Project in Visual Basic

Step 2: Create a User Interface

Step 3: Customize Looks and Behavior

Concepts

Closer Look: Understanding Properties, Methods, and Events

Other Resources

Creating Your First Visual Basic Program