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 Option Button control in conjunction with these container controls to present multiple choices to a user.

Using Panels and Group Boxes

Controls can be placed on a form because the form is a container objectan object that can host controls. A form isn't the only type of container, however. Some controls act as containers as well, and a container can 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.

By the Way

For the most part, the Panel control is a slimmed-down version of the Group Box control, so I won't discuss the Panel in depth. If you need a basic container control without the additional features offered by the Group Box control (such as a border and caption), use the Panel control. The primary exception to this is that the panel offers scrolling capabilities just like those found on forms, which group boxes do not support.


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

Try clicking in the center of the group box and dragging it around like you would another type of control: You can't. Think of the group box like a mini formyou can't click and drag a form to move it around. Clicking and dragging on a group box would allow you to lasso any controls placed on the group boxthe same behavior you experience on a form. To drag a group box, click and drag the little image with the four arrows on it (see Figure 7.9).

Figure 7.9. Click and drag this box to move a group box.


Set the properties of the group box as follows:

Property

Value

Name

grpDefaultBackcolor

Location

105,112

Size

200,72

Text

Default Picture Background Color


Your group box should now look like the one in Figure 7.10.

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


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 section, "Working with Radio Buttons," demonstrates the benefits of using a group box as a container.

Working with Radio Buttons

Check boxes are excellent controls for displaying True/false and yes/no values. However, check boxes work independently of one another; if you have five check boxes on a form, each one of them could be checked or uncheckedin any combination. Radio buttons, on the other hand, are mutually exclusive to the container on which they're placed. This means that only one radio button per container can 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 for your Options form.

You can perform any of the following actions to place a control on a group box:

  • Draw the control directly on the group box.

  • Drop the control on the group box.

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

You're going to use the second method: dropping a new control directly on the group box. Follow these steps:

1.

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

2.

Release the mouse when you're over the group box.

3.

Move the radio button around by clicking and dragging it. Don't drag the radio button off of the container, or it will be moved to the new container or form over which it is placed when you release the mouse.

Set the properties of the radio button as follows:

Property

Value

Name

optBackgroundDefault

Checked

True

Location

6,19

Text

Default Gray


Note that the Location property is always in reference to the container object. If the control is on a form, it's relative to the upper-left corner of the form. If the control is on a group box, the location is relative to the upper-left corner of the group box, etc. You're now going to copy this radio button and paste a copy of the control on the group box:

1.

Right-click the radio button and choose Copy from its context menu.

2.

Click the group box to select it.

3.

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

optBackgroundWhite

Checked

False

Location

6,42

Text

White


You might notice that both radio buttons appear selected in the designer. At runtime, only one of them will appear selected.

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

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


Click the Options button to display your Options form and take a look at the radio buttons. The first radio button is selected, so click the second radio button (White) and notice how the first radio button becomes deselected automatically (its Checked property is set to False). Two radio buttons are sufficient to demonstrate mutual exclusivity, but be aware that you could add as many radio buttons to the group box as you want 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 on another container.

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.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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