Visual Basic 6 Black Book:Command Buttons, Checkboxes, And Option Buttons
example, a Finish button or a Yes button), but a Cancel button is just about required in dialog
boxes. You should always have a Cancel button to let the user close the dialog box in case he has
opened it by mistake or changed his mind.
Command buttons do have a Cancel property, and Microsoft recommends that you set it to True if
you are making a command button into a Cancel button. Only one button can be a Cancel button
in a form.
However, there doesn’t seem to be much utility in making a command button into a Cancel button.
There’s nothing special about that button, really—it won’t automatically close a dialog box, for
example—except for one thing: when the user hits the Esc key, the Cancel button is automatically
clicked. Using the Esc key is one way users have of closing dialog boxes, but it’s not a very
compelling reason to have a separate Cancel property for buttons.
Tellingly, the Cancel button in the predefined dialog box that comes with Visual Basic (you can
add it when you select Project|Add Form) does not have its Cancel property set to True.
Getting A Checkbox’s State
You’ve added all the checkboxes you need to your new program, WinBigSuperCasino, and you’ve
connected those checkboxes to Click event handlers. But now there’s a problem—when the users
set the current amount of money they want to bet, you need to check if they’ve exceeded the limit
they’ve set for themselves. But they set their limit by clicking another checkbox. How can you
determine which one they’ve checked?
You can see if a checkbox is checked by examining its Value property (Visual Basic does have a
Checked property, but that’s only for menu items, a fact that has confused more than one
programmer). Here are the possible Value settings for checkboxes:
• 0— Unchecked
• 1— Checked
• 2— Grayed
Here’s an example; in this case, we will change a command button’s caption if a checkbox,
Check1, is checked, but not otherwise:
Private Sub Command1_Click()
If Check1.Value = 1 Then
Command1.Caption = "The check mark is checked"
Setting A Checkbox’s State
Your new program, SuperSandwichesToGoRightNow , is just about ready, but there’s one hitch.
You use checkboxes to indicate what items are in a sandwich (cheese, lettuce, tomato, and more)
to let users custom-build their sandwiches, but you also have a number of specialty sandwiches
with preset ingredients. When the user selects one of those already-built sandwiches, how do you
set the ingredients checkboxes to show what’s in them?
file:///E|/Program%20Files/KaZaA/My%20Shared%...Basic%20-%20%20Black%20Book/ch07/244-247.html (2 of 4) [7/31/2001 8:58:35 AM]