|
|
 |
 |
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 7 Command Buttons, Checkboxes, And Option Buttons
- If you need an immediate solution to:
- Setting A Buttons Caption
- Setting A Buttons Background Color
- Setting Button Text Color
- Setting Button Fonts
- Reacting To Button Clicks
- Creating Button Control Arrays
- Resetting The Focus After A Button Click
- Giving Buttons Access Characters
- Setting Button Tab Order
- Disabling Buttons
- Showing And Hiding Buttons
- Adding Tool Tips To Buttons
- Resizing And Moving Buttons From Code
- Adding A Picture To A Button
- Adding A Down Picture To A Button
- Adding Buttons At Runtime
- Passing Buttons To Procedures
- Handling Button Releases
- Making A Command Button Into A Cancel Button
- Getting A Checkboxs State
- Setting A Checkboxs State
- Grouping Option Buttons Together
- Getting An Option Buttons State
- Setting An Option Buttons State
- Using Graphical Checkboxes And Radio Buttons
- Using Checkboxes And Option Buttons Together
In Depth
In this chapter, were going to take a look at what are arguably the most popular controls in Visual Basic: buttons. These include command buttons, checkboxes, and option buttons.
Command buttonsthe plain buttons that you simply click and releaseare the most common type of buttons. These are the buttons you see everywhere in Visual Basic applications. They are usually just rounded, rectangular, gray buttons with a caption.
Checkboxes are also familiar controls. You click a checkbox to select it and click it again to deselect it. When you select a checkbox, a checkmark appears in it, indicating that the box is indeed selected.
Option buttons, also called radio buttons, are like checkboxes in that you select and deselect them. However, they are round, whereas checkboxes are square, and you usually use option buttons together in groups. In fact, thats the functional difference between checkboxes and option buttons: checkboxes can work independently, but option buttons are intended to work in groups. When you select one option button in a group, the others are automatically deselected. For example, you might use checkboxes to select trimmings on a sandwich (of which there can be more than one), whereas you might use option buttons to let the user select one of a set of exclusive options, like the current day of the week.
You use tools in the toolbox to add command buttons, checkboxes, and option buttons to a form. In the toolbox in Figure 7.1, the Command Button tool is third down on the right, the Checkbox tool is fourth down on the left, and the Option Button tool is fourth down on the right.
Figure 7.1 The Command Button tool, the Checkbox tool, and the Option Button tool.
How This Chapter Works
Because the three different types of buttons have many similar characteristics, it makes sense to cover them in the same chapter. In fact, the three types of buttons have so many properties and methods in common that when covering such topics, well refer to command buttons, checkboxes, and option buttons collectively as buttons.
For example, all three controls have a Caption property, so when we cover how to set captions in those controls, well refer to them collectively as buttons. The title of that topic, then, is Setting A Buttons Caption. If were covering something that refers to one type of button exclusively, Ill indicate that in the title of the topic, for example, Grouping Option Buttons Together. In this way, well be able to address both what all the buttons have in common and what makes them useful independently.
Thats all the introduction we needwell turn to the Immediate Solutions now.
Immediate Solutions
Setting A Buttons Caption
You use a buttons Caption property to set its caption. This property is available at both design time and runtime.
After you add a button to a form, you set its caption by placing the appropriate text in the Caption property in the Properties window. You can also change the buttons caption at runtime, of course. As an example, well use our tic-tac-toe program from Chapter 1:
Private Sub Form_Load()
xNow = True
End Sub
Private Sub Command_Click(Index As Integer)
If xNow Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
xNow = Not xNow
End Sub
TIP: Its useful to be able to change the captions of buttons. For example, if a command buttons caption reads Connect To Internet, then when youre connected you could change the buttons caption to Disconnect From Internet, and disconnect from the Internet when the button is clicked.
Setting A Buttons Background Color
Youve got your program running at last, but now the Aesthetic Design Department is on the phone. The emergency window in your program is colored redwhy not the Panic button in the middle of that window also?
So, how do you do that? You can use the buttons BackColor property, as shown in Figure 7.2. Note that you also have to set the buttons Style property to Graphical (which has a numeric value of 1). Well see more about graphical buttons later in this chapter. Here, were setting the background color of a button at design time, and two sets of colors are available: a set of standard Visual Basic control colors (like Button Face, Button Shadow, and so on), and a palette of colors.
Figure 7.2 Setting a buttons background color.
You can also set the buttons BackColor property at runtime, setting it to a value using the RGB() function, which takes three parameters (0 to 255) for the red, green, and blue color values you want to set. Here, we change the color of a graphical button to red:
Command1.BackColor = RGB(255, 0, 0)
|