Mastering VB Controls
Written by Mike James   
Article Index
Mastering VB Controls
Lists and ComboBoxes
Example - Improved Mortage Calculator
Processing the data

There are lots of different controls that you can use to create a user interface and if you want to master Visual Basic you need to know what tools are in your toolbox. Continuing our introduction to Visual Basic 2010.

When you first use Visual Basic there is a great deal of pleasure to be had from the way you can draw a button on a form and start writing event handling code that makes the button do something.

Visual Basic has a lot of controls that you can use to build a user interface.  As you progress you discover that none are quite as straightforward as the button. They are not difficult but a few words of explanation are necessary to make sure that you get full use out of them. So let's go beyond the button -

Step One - Variations on a button

A command button is easy to use because it is simple - you just click on it to make something happen.

There are two other types of button which behave differently - CheckBoxes and RadioButtons. Both buttons have a Checked property which can be True or False depending on whether or not the button is ticked (selected) or not. The CheckBox shows an X when it is selected and the RadioButton shows a circle.

You can use either type of button to allow the user to select options from a list. The big difference between the two types of button is that the RadioButtons on a form work as a group and only one of them can be selected at a time. CheckBoxes work independently of one-another and can be checked or unchecked as the user likes.

This means that you should use CheckBoxes for options that can be chosen independently of one another and option buttons for a set of mutually exclusive choices.

 

checkbox

 

For example, if you are designing a form to allow a user to order breakfast (presumably over the Internet!) you might have three CheckBoxes for bacon, eggs and toast as these can be ordered in any combination but you might restrict their choice to one of tea or coffee by using two option buttons.

breakfast

 

Each button also also has a range of events that it can respond to but in the main you don't need to write event handlers for CheckBoxes or RadioButtons because their values are processed when the user has finished filling in all of the details on the form. In most cases you have a standard button which is clicked to indicate that the user has finished entering data and making selections.

 

Step Two - Containers

Any  RadioButtons  that you place on a form work together - i.e. only one of them can be selected at a time.

You can interrogate the Checked property of each button to find out which one is pressed - nothing could be simpler.

Now imagine that we need to add some RadioButtons to give a user the choice of a daily newspaper as well as what to drink at breakfast. If all of the option buttons are on the same form the user will be a little surprised to find that clicking on the "Tea" button immediately cancels any request for a newspaper. The point is that ALL of the option buttons on a form work together.

The solution to this limitation is the GroupBox control - which unless you know about RadioButtons look as if it has no real function in life. The GroupBox  acts as a sort of mini form within another  form. Any controls that you draw on the GroupBox are located with reference to the GroupBox. For example, if you drag the GroupBox to a new location then its controls move with it. If you delete a frame then all of its controls are deleted as well.

The GroupBox is our first example of a control that is designed to hold or contain other controls - it is a container object. There are a number of other container objects that we will meet later.

You can see that the GroupBox is the ideal way of creating groups of controls. In the case of RadioButtons it is also the only way of creating more than one group of buttons that work together. Each set of RadioButtons in a particular GroupBox work as a group. So to create one set of buttons for tea or coffee and another set for newspaper we need one or two GroupBoxes. 

 

group

If you have already created a set of  RadioButtons, or any other control for that matter, and wish that you had put them inside a GroupBox  then you can - but it isn't just a matter of dragging the controls over the GroupBox. If you do this you will end up with something that looks as if the controls are part of the GroupBox but they are not as you can soon prove when you try moving the GroupBox. To make the controls belong to the GroupBox you have to use Edit,Cut to remove the controls to the clipboard, select the GroupBox and then while it is selected use Edit,Paste to paste the controls into the GroupBox. Notice that this doesn't work if you paste while the GroupBox isn't selected.

<ASIN:0470502223>

<ASIN:0735626693>

<ASIN:0470532874 >

<ASIN:0136113400 >