Creating Containers and Groups of Option Buttons

   

Creating Containers and Groups of Option Buttons

In this section, you'll learn how to create containers for groups of controls using panels and group boxes. You'll also learn how to use the Check Box and Option Button controls in conjunction with these container controls to present multiple choices to a user .

Begin by creating a new Windows Application titled Options. Change the name of the default form to fclsOptions and set the form's Text property to Options. Next , change the entry point Main() to reference fclsOptions instead of Form1.

Using Panels and Group Boxes

graphics/newterm.gif

Controls can be placed on a form because the form is a container objectan object that can contain controls. A form is not the only type of container, however. Some controls act as containers as well, and a container may host one or more other containers. The Panel and Group Box controls are both container controls that serve a similar purpose, yet each is more suited to a particular application.

graphics/bookpencil.gif

The Panel control is a slimmed-down version of the Group Box control, so I won't be discussing it in depth. If you need a very basic container control without the additional features offered by the group box, such as a border and a caption, use the Panel control.

The group box is a container control with properties that let you create a border (frame) and caption. Add a new group box to your form now by double-clicking the GroupBox item in the toolbox. When you create a new group box, it has a border by default, and its caption is set to the name of the control (see Figure 7.12).

Figure 7.12. A group box acts like a form within a form.

graphics/07fig12.jpg


Set the properties of the group box as follows :

Property Value
Name grpMyGroupBox
Location 48,16
Size 200,168
Text This is a group box

The group box is a fairly straightforward control. Other than defining a border and displaying a caption, the purpose of a group box is to provide a container for other controls. The next two sections, "Presenting Yes/No Options Using Check Boxes" and "Working with Radio Buttons," help demonstrate the benefits of using a group box as a container.

Presenting Yes/No Options Using Check Boxes

The check box is used to display true/false values on a form. You're now going to add a check box to your formbut not in the way you have been adding other controls. As you know by now, double-clicking the CheckBox item in the toolbox will place a new Check Box control on the form. However, this time you're going to place the check box on the Group Box control.

To place a control on a group box, you can use one of the following techniques:

  • Add the control to the form, cut the control from the form, select the group box, and paste the control on the group box.

  • Draw the control directly on the group box.

  • Drop the control on the group box.

You're going to use the third methoddropping a new control directly on the group box. Follow these steps:

  1. Click the group box to select it.

  2. Click the CheckBox item in the toolbox and drag it to the group box.

  3. Release the mouse when you are over the group box.

You should now have a check box on your group box like the one shown in Figure 7.13.

Figure 7.13. Container controls hold other controls.

graphics/07fig13.jpg


Move the check box around by clicking and dragging it. You'll notice that you can't move the check box outside the group box's boundaries. This is because the check box is a child of the group box, not of the form. Set the properties of the check box as follows:

Property Value
Name chkMyCheckBox
Location 16,24
Size 120,24
Text This is a check box

When the user clicks the check box, it changes its visible state from checked to unchecked. To see this behavior, press F5 to run the project and click the check box a few times.

When you're done experimenting, stop the running project. To determine the state of the check box in code, use its Checked property. You can also set this property at design time using the Properties window.

Working with Radio Buttons

Check boxes are excellent controls for displaying true/false values. Check boxes work independently of one another, however. If you have five check boxes on a form, each of them could be checked or unchecked. Radio buttons, on the other hand, are mutually exclusive to the container on which they are placed. This means that only one radio button per container may be selected at a time. Selecting one radio button automatically deselects any other radio buttons on the same container. Radio buttons are used to offer a selection of items to a user when the user is allowed to select only one item. To better see how mutual exclusivity works, you're going to create a small group of radio buttons.

Click the RadioButton item in the toolbox to begin dragging, move the pointer over the group box, and then release the button to drop a new radio button on the group box. Set the properties of the radio button as follows:

Property Value
Name optOption1
Location 19,65
Size 104,24
Text This is option 1

You're going to copy this radio button and paste a copy of the control on the group box. Begin by right-clicking the radio button and choosing Copy from its context menu. Next, click the group box to select it, right-click the group box, and choose Paste from its context menu to create a new radio button. Set the properties of the radio button as follows:

Property Value
Name optOption2
Checked true
Location 19,96
Text This is option 2

Now that you have your two radio buttons (see Figure 7.14), run the project by pressing F5.

Figure 7.14. Radio buttons restrict a user to selecting a single item.

graphics/07fig14.jpg


Click the first radio button to select it, and notice how the second radio button becomes deselected automatically (its Checked property is set to false). Two radio buttons are sufficient to demonstrate the mutual exclusivity, but be aware that you could add as many radio buttons to the group box as you care to and the behavior would be the same. The important thing to remember is that mutual exclusivity is shared only by radio buttons placed on the same container. To create radio buttons that behave independently of one another, you would need to create a second set. You could easily create a new group box (or panel for that matter) and place the second set of radio buttons on the new container. The two sets of radio buttons would behave independently of one another, but mutual exclusivity would still exist among the buttons within each set.

Stop the running project and save your work.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net