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

Visual Basic .NET 2008 Express - About Programming
Integrating HelloWorld into the Program

By , About.com Guide

May 29 2009

The HelloWorld form code will take a few more changes. Select the Design window and add a GroupBox from the Toolbox. Here's what the VB.NET Express Help says about this control:

Use a GroupBox to logically group a collection of controls on a form. The GroupBox is a container control that can be used to define groups of controls.

The typical use for a GroupBox is to contain a logical group of RadioButton controls. If you have two GroupBoxes, each of which contain several option buttons (also known as radio buttons), each group of buttons is mutually exclusive, setting one option value per group.

That's exactly what we're going to do. Add three RadioButton controls inside the GroupBox. Remember to rename them as they're added. I used the name LanguageGroup for the GroupBox and English, Español, and Deutsch for the RadioButton controls. Change the Checked property of the one you want as a default selection to True.

In Part 1, we learned about event subroutines. When the Button control in the first program was clicked, the Click event was triggered and the DisplayHello_Click subroutine was automatically executed. We'll continue to use this code in our new program but instead of displaying the message directly, the subroutine in HelloModule will be called and the variable Language is passed to it so the subroutine in HelloModule will know what language to use. Replace the line of code in the DisplayHello_Click sub with:

HelloModule.SayHello(Language)

In addition, we have to capture the language selection whenever one of the RadioButton controls is selected. We'll use the CheckedChanged event for each of the three RadioButton controls to do that.

VB programmers almost never enter code like a base event subroutine "by hand" because it's too easy to make a mistake and also because Visual Basic will create most of the code for you. If you double-click the control in the designer view, VB will enter the code and switch to code view.

But there's also another way.

There are two "drop down" lists at the top of the code window. The left one is the "Class Name" selection list and the right one is the "Method Name" selection list. If you select "English" in the "Class Name" drop down list and "CheckedChanged" in the "Method Name" drop down list, Intellisense will create the basic event subroutine for you. Then all you have to do is enter one line of code (Language = "English") in the subroutine.

Here's the code for just one of them. The others are similar. I reformatted the lines with continuation characters (the space and underscore at the end) just to make it easier to read.

Private Sub English_CheckedChanged( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles English.CheckedChanged
   Language = "English"
End Sub

Notice that we need a variable (Language) to hold the language selection. Because it must be referenced by all of the CheckedChanged events, I declared it outside the subroutines. This gives the variable module scope. We'll discuss what is called scope of variables in greater detail later too. In my program, I also changed the Text property of the controls to make everything look better.

The new code and the HelloWorld form are shown below:

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

At this point, you should be able to run the new, modular Hello World by pressing F5 or clicking the Run icon. Click here to download the complete code.

There are a lot of improvements that can be made. For example, the Button control is always in English even though the rest of the form is in the target language. See if you can figure out how to change the button whenever a different language selection is made.

In the next part of the course: What's New With Visual Basic .NET Express, we get up to speed on VB Express and what's new and different in it.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - About Programming

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

All rights reserved.