Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Frames and Option Buttons

Figure 11.3 shows an application with two sets of option buttons. The option buttons let you select a computer type and operating system. Figure 11.3 seems to violate the option button's primary rule: More than one option button is selected at the same time (the Pentium option button and the Windows 95 option).

11fig03.gif

Figure 11.3 Two option buttons are set.

Sometimes a form will need several sets of option buttons, just like the form in Figure 11.3. In each set the user should be allowed to select only one option button, but one option button should be set from each set at the same time. Therefore, you must revise the previous rule, which states that only one option button can be set at one time. The truth is that only one option button inside a frame can be set at one time.

A frame is a rectangular region on a form that holds other controls and groups the controls into a single set. It is a rectangular outline with an optional title. When you want to place multiple sets of option buttons on a form, first place the frame or frames onto the form. (You can place any control on a frame, but the frame especially helps group option buttons so you can offer multiple option button sets.)

The frame control does support properties that determine the frame's look and caption and a frame does support a few events, but most programmers use the frame as a holding place to group other controls. After you place controls in a frame, you can move the frame and all the frame's controls move with it. Therefore, adjusting framed controls is relatively easy to do.

Figure 11.4 shows an application that contains three frames that determine how text appears inside a label. The user can select only one option button inside each frame. As soon as the user changes one of the options, the option button's Click() event responds to the change and sets the Label property accordingly. Listing 11.1 contains the complete form module code that takes care of the user's action. The label is initialized in the Form_Load() event procedure (the procedure that executes right before the user sees the form), and the remaining event procedures are the responses to various user clicks on the form's controls. The controls are named well enough so that you will know where the controls appear in Figure 11.4.

11fig04.gif

Figure 11.4 A form with three frames.

Example 11.1. The framed option button code.

 1: Private Sub Form_Load()
 2: ' Initialize the label's text
 3: Dim strLabel1 As String
 4: Dim strLabel2 As String
 5: Dim strLabel3 As String
 6: Dim strLabel4 As String
 7:
 8: strLabel1 = "Use frames if you want "
 9: strLabel2 = "to group options together. "
10: strLabel3 = "Each frame forms one set "
11: strLabel4 = "of option buttons."
12:
13: lblFrames.Caption = strLabel1 & strLabel2 & _
14: strLabel3 & strLabel4
15:
16: ' Set the label's properties
17: lblFrames.FontItalic = True
18: optItalicTrue.Value = True
19:
20: lblFrames.FontUnderline = True
21: optUnderTrue.Value = True
22:
23: lblFrames.ForeColor = vbBlue
24: optBlue.Value = True
25: End Sub
26:
27: Private Sub optItalicTrue_Click()
28: lblFrames.FontUnderline = True
29: End Sub
30:
31: Private Sub optItalicFalse_Click()
32: lblFrames.FontUnderline = False
33: End Sub
34:
35: Private Sub optRed_Click()
36: lblFrames.ForeColor = vbRed
37: End Sub
38:
39: Private Sub optBlue_Click()
40: lblFrames.ForeColor = vbBlue
41: End Sub
42:
43: Private Sub optGreen_Click()
44: lblFrames.ForeColor = vbGreen
45: End Sub
46:
47: Private Sub optUnderTrue_Click()
48: lblFrames.FontItalic = True
49: End Sub
50:
51: Private Sub optUnderFalse_Click()
52: lblFrames.FontItalic = False
53: End Sub
54:
55: Private Sub cmdExit_Click()
56: Unload Me
57: End
58: End Sub

Table 11.1. The color named literals.

Literal Color
vbBlack Black
vbRed Red
vbGreen Green
vbYellow Yellow
vbBlue Blue
vbMagenta Magenta
vbCyan Cyan
vbWhite White

Share ThisShare This

Informit Network