1. About.com
  2. Computing & Technology
  3. Visual Basic

Discuss in my forum

Visual Basic .NET 2010 Express - About Programming

Integrating HelloWorld into the Program

By , About.com Guide

The HelloWorld form code will take a few more changes. Select the Design window and add a GroupBox from the Toolbox. (You'll find it in the Containers section.) 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 group box 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 and change the Text property as they're added. I used the name grpLanguageGroup for the GroupBox and rdbEnglish, rdbEspañol, and rdbDeutsch 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 btnDisplayHello control in the first program was clicked, the Click event was triggered and the btnDisplayHello_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 will be passed to it so the subroutine in HelloModule will receive the data about what language to use. Replace the line of code in the btnDisplayHello_Click sub with:


HelloModule.SayHello(Language)

After you make this change, you'll notice that Language is flagged as an error by VB.NET. This happens a lot in the middle of coding operations. You're OK as long as you know what to do to fix it and just haven't entered the necessary code yet. In this case, 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. We learned earlier that 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 "rdbEnglish" in the "Class Name" drop down list and "CheckedChanged" in the "Method Name" drop down list, Intellisense will create the base event subroutine for you. Then all you have to do is enter one line of code (Language = "English") in the subroutine and you don't even have to switch to the design view.

Here's the code for just one of them. The others are similar.


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

We still need a variable (Language) to hold the language selection. Because it must be referenced by all of the CheckedChanged events, it's declared outside the subroutines. This gives the variable module scope. We'll discuss what is called scope of variables in greater detail later too.

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 2010.NET Express, we get up to speed on VB Express and what's new and different in it.

©2012 About.com. All rights reserved. 

A part of The New York Times Company.