Export (0) Print
Expand All

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.

video For a video demonstration, see Video: 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.

    NoteTip

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

|

End Sub

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 Closer Look: Understanding Properties, Methods, and Events

If you don't understand the code, don't worry—you will learn a lot more about writing 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

Community Additions

ADD
Show:
© 2014 Microsoft