1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial
Adding Components and Running the Program

By , About.com Guide

May 3 2009

Now we're going to add two new controls to the form. Controls can 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 position by clicking the icon as shown in the illustration below. The two controls that will be added are the Button and the Label. A circle is drawn around them in the illustration.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

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 and it will be added in the upper left corner of the form. 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 "DisplayHello" for the Button and "HelloWorldLabel" for the Label.) Finally, change the Text property for the Button (I used "Display Hello".) and delete the Text property for the Label. You might notice that the Label might seem to disappear entirely from the display of the form when you delete the Text property. To find it again, click the down arrow in the control list at the top of the Properties window. The illustration below shows all of this detail.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The last thing we need to do is add the code that will display "Hello World!" in the HelloWorldLabel control when we click the DisplayHello 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 Visual Basic Express. Notice that a Class and 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 DisplayHello Button Name property has been combined with the Click event using an underscore character. This means that when the DisplayHello Button is clicked, the code you put in this subroutine will be executed. (Technically, the procedure named in the Handles clause at the end of the statement is executed. They will usually be the same, however.) I entered the code:

HelloWorldLabel.Text = "Hello World!"

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. So the Text property of the HelloWorldLabel control is assigned the value "Hello World!" when the DisplayHello Button is clicked. Here's the completed program:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Now let's try out the program and see if it works. To run the program, click the Start Debugging icon in the Visual Basic Express toolbar. The previous illustration shows that too. The form with our two controls is displayed (the Label doesn't show until there is something in the Text property). Click the DisplayHello Button and the Text is displayed. I also changed the Font property of the Label to make it show up a little better.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

If you've used VB 6 or previous versions, this will look very familiar to you, but trust me, there are some big differences that we'll discuss in later lessons.

The goal of this lesson has been to get a working copy of Visual Basic 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.

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 Label Font property to match my example or 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 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.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial

©2009 About.com, a part of The New York Times Company.

All rights reserved.