Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Option Buttons
- Frames and Option Buttons
- Check Boxes
- Scrollbars
- VB's Clock: The Timer Control
- Summary
- Q&A
- Workshop
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
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).
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.
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 |
Check Boxes | Next Section

Account Sign In
View your cart