1. Computing

Visual Basic .NET 2010 Express - A "From the Ground Up" Tutorial

Adding Components and Running the Program

By

Controls are objects that do things for you in your project. They can usually be found in the Toolbox. The Toolbox is a good example of an Auto Hide window; a window that will automatically display when you move your mouse over the tab for it. You can pin an Auto Hide window in the open position by clicking the icon as shown in the illustration below.

--------
Click Here to display the illustration
--------

To add the control to the form, just click and drag it to where you want it on the form. You can also double click the control to add it. After the controls have been added, you can resize the controls and change their properties. Add one Button and one Label to the form. Change the names and position them where you want them. (I used "btnDisplayHello" for the Button and "lblHelloWorld" for the Label.) Finally, change the Text property for the Button. (I used "Display Hello". I also resized the Button slightly.)

The last thing we need to do is add the code that will display "Hello World!" in the Label control when we click the btnDisplayHello Button. To enter code, we use a different window called the code window. The easiest way to display the code window, and get Visual Basic to give you a running start, is to double click the control that you want to use to perform some action. This displays a default event subroutine for that control with some of the code already added for you by VB.NET.

Notice that a Class and a Sub (that is, a "subroutine") have been created for you and that they have the names that you gave to the Form and the Button. In addition the btnDisplayHello Name property has been combined with the name of this event, that is "Click", using an underscore character. This means that when the program is running and btnDisplayHello is clicked, the code you put in this subroutine will be executed.

Technically, in the Click event subroutine, the event named in the Handles clause at the end of the statement is the one that triggers the execution of this subroutine. They will usually be the same, however.

Properties (and methods, which we'll also learn about later) for controls are identified by the dot operator. The Name of a control is connected to a method or a property with a dot to indicate that the property or method is part of that control. So the Text property of the lblHelloWorld control is assigned the value "Hello World!" when the btnDisplayHello Button is clicked.


Public Class HelloForm
    Private Sub btnDisplayHello_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs
        ) Handles btnDisplayHello.Click
        lblHelloWorld.Text = "Hello World"
    End Sub
End Class

If you have worked with previous versions of VB.NET, one of the changes for VB.NET 2010 that you may notice is on display here. Previous versions required a "line continuation character" (That's a space followed by an underscore at the end of the line) to keep lines short by spreading a statement over several lines. The Sub statement here is spread over four lines but you don't have to use them now. (They still work if you want to use them.)

Now let's try out the program and see if it works. To run the program, click the Start Debugging icon in the toolbar or press F5. Click the btnDisplayHello Button and Hello World is displayed in the Label. Here's the completed program:

--------
Click Here to display the illustration
--------

The goal of this lesson has been to get a working copy of Visual Basic 2010 Express on your computer and make sure that even people who haven't written a single line of programming code before could write a program. In future lessons, I won't go through the steps in this much detail. I'll assume that you know how to use Visual Basic and how to change properties and enter programming code. In future lessons, I'll also provide downloads for the programs that we create, but since the point of this program was to make sure everyone could enter and run a program themselves, I haven't done that this time.

Extra Credit for the Extra Ambitious

So if you have any doubts at all about what you're doing, try changing the Hello World program to do something else.

  • Change the Font property to see what something else looks like.
  • Add a Textbox to the form and add code that will transfer text in the TextBox to the Label.
  • Change the title on the form when the Button is clicked instead of putting the text in a Label.

In part 2 of the tutorial, Visual Basic Fundamentals, we cover two topics that you need to get started. The first is where to find more information. Online learning is different from reading a book because you literally have millions of pages of information available to you at the click of a mouse. So it doesn't make sense to cover all of the fundamentals here. But it does make sense to give you a foundation to work from so the second topic is the Visual Basic syntax and basic structures. This section of the tutorial covers the basics that you need to know to write a program without going into the details that you can and should get directly from Microsoft anyway.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2010 Express - A "From the Ground Up" Tutorial

©2014 About.com. All rights reserved.