CheckBox
es and RadioButton
sC# has two types of state buttons that can be in the on/off or true/false states—CheckBox-
es and RadioButton
s. Like class Button
, classes CheckBox
and RadioButton
are derived from class ButtonBase
.
CheckBox
esA CheckBox
is a small square that either is blank or contains a check mark. When the user clicks a CheckBox
to select it, a check mark appears in the box. If the user clicks the CheckBox
again to deselect it, the check mark is removed. You also can configure a CheckBox
to toggle between three states (checked, unchecked and indeterminate) by setting its Three-State
property to true
. Any number of CheckBox
es can be selected at a time. A list of common CheckBox
properties and a common event appears in Fig. 14.25.
CheckBox
properties and an event.CheckBox properties and events |
Description |
---|---|
Common Properties | |
Appearance |
By default, this property is set to Normal , and the CheckBox displays as a traditional checkbox. If it’s set to Button , the CheckBox displays as a Button that looks pressed when the CheckBox is checked. |
Checked |
Indicates whether the CheckBox is checked (contains a check mark) or unchecked (blank). This property returns a bool value. The default is false (unchecked). |
CheckState |
Indicates whether the CheckBox is checked or unchecked with a value from the CheckState enumeration (Checked , Unchecked or Indeterminate ). Indeterminate is used when it’s unclear whether the state should be Checked or Unchecked . When CheckState is set to Indeterminate , the CheckBox is usually shaded. |
Text |
Specifies the text displayed to the right of the CheckBox . |
ThreeState |
When this property is true , the CheckBox has three states—checked, unchecked and indeterminate. By default, this property is false and the CheckBox has only two states—checked and unchecked. When true , Checked returns true for both the checked and indeterminate states. |
Common Event | |
CheckedChanged |
Generated any time the Checked or CheckState property changes. This is a CheckBox ’s default event. When a user double clicks the CheckBox control in design view, an empty event handler for this event is generated. |
The program in Fig. 14.26 allows the user to select CheckBox
es to change a Label
’s font style. The event handler for one CheckBox
applies bold and the event handler for the other applies italic. If both CheckBox
es are selected, the font style is set to bold and italic. Initially, neither CheckBox
is checked.
The boldCheckBox
has its Text
property set to Bold
. The italicCheckBox
has its Text
property set to Italic
. The Text
property of outputLabel
is set to Watch the font style change
. After creating the controls, we define their event handlers. Double clicking the CheckBox
es at design time creates empty CheckedChanged
event handlers.
To change a Label
’s font style, set its Font
property to a new Font
object (lines 22– 23 and 31–32). Class Font
is in the System.Drawing
namespace. The Font
constructor used here takes a Font
and new style as arguments. The argument outputLabel.Font
uses outputLabel
’s original font name and size. The style is specified with a member of the FontStyle
enumeration, which contains Regular
, Bold
, Italic
, Strikeout
and Underline
. (The Strikeout
style displays text with a line through it.) A Font
object’s Style
property is read-only, so it can be set only when the Font
object is created.
Styles can be combined via bitwise operators—operators that perform manipulation on bits of information. Recall from Chapter 1 that all data is represented in the computer as combinations of 0s and 1s. Each 0 or 1 represents a bit. The FontStyle
(namespace System.Drawing
) is represented as a set of bits that are selected in a way that allows us to combine different FontStyle
elements to create compound styles, using bitwise operators. These styles are not mutually exclusive, so we can combine different styles and remove them without affecting the combination of previous FontStyle
elements.
We can combine the various font styles, using either the logical OR (|
) operator or the logical exclusive OR (^
) operator (also called XOR). When the logical OR operator is applied to two bits, if at least one bit of the two has the value 1, then the result is 1. Combining styles using the logical OR operator works as follows. Assume that FontStyle.Bold
is represented by bits 01
and that FontStyle.Italic
is represented by bits 10
. When we use the logical OR (|
) to combine the styles, we obtain the bits 11
.
01 = Bold
10 = Italic
--
11 = Bold and Italic
The logical OR operator helps create style combinations. However, what happens if we want to undo a style combination, as we did in Fig. 14.26?
The logical exclusive OR operator enables us to combine styles and to undo existing style settings. When logical exclusive OR is applied to two bits, if both bits have the same value, then the result is 0. If both bits are different, then the result is 1.
Combining styles using logical exclusive OR works as follows. Assume, again, that FontStyle.Bold
is represented by bits 01
and that FontStyle.Italic
is represented by bits 10
. When we use logical exclusive OR (^
) on both styles, we obtain the bits 11
.
01 = Bold
10 = Italic
--
11 = Bold and Italic
Now, suppose that we’d like to remove the FontStyle.Bold
style from the previous combination of FontStyle.Bold
and FontStyle.Italic
. The easiest way to do so is to reapply the logical exclusive OR (^
) operator to the compound style and FontStyle.Bold
.
11 = Bold and Italic
01 = Bold
--
10 = Italic
This is a simple example. The advantages of using bitwise operators to combine FontStyle
values become more evident when we consider that there are five FontStyle
values (Bold
, Italic
, Regular
, Strikeout
and Underline
), resulting in 16 FontStyle
combinations. Using bitwise operators to combine font styles greatly reduces the amount of code required to check all possible font combinations.
In Fig. 14.26, we need to set the FontStyle
so that the text appears in bold if it was not bold originally, and vice versa. Line 23 uses the bitwise logical exclusive OR operator to do this. If outputLabel.Font.Style
is bold, then the resulting style is not bold. If the text is originally italic, the resulting style is bold and italic, rather than just bold. The same applies for FontStyle.Italic
in line 32.
If we didn’t use bitwise operators to compound FontStyle
elements, we’d have to test for the current style and change it accordingly. In boldCheckBox_CheckedChanged
, we could test for the regular style and make it bold, test for the bold style and make it regular, test for the italic style and make it bold italic and test for the italic bold style and make it italic. This is cumbersome because, for every new style we add, we double the number of combinations. Adding a CheckBox
for underline would require testing eight additional styles. Adding a CheckBox
for strikeout would require testing 16 additional styles.
RadioButton
sRadio buttons (defined with class RadioButton
) are similar to CheckBox
es in that they also have two states—selected and not selected (also called deselected). However, RadioButton
s normally appear as a group, in which only one RadioButton
can be selected at a time. Selecting one RadioButton
in the group forces all the others to be deselected. Therefore, RadioButton
s are used to represent a set of mutually exclusive options (i.e., a set in which multiple options cannot be selected at the same time).
Use RadioButton
s when the user should choose only one option in a group. Use Check-Box
es when the user should be able to choose multiple options in a group.
All RadioButton
s added in a container are in the same group. To divide RadioButton
s into several groups, they must be added to separate containers, such as GroupBox
es or Panel
s. RadioButton
’s common properties and a common event are listed in Fig. 14.27.
RadioButton
properties and an event.RadioButton properties and an event |
Description |
---|---|
Common Properties | |
Checked |
Indicates whether the RadioButton is checked. |
Text |
Specifies the RadioButton ’s text. |
Common Event | |
CheckedChanged |
Generated every time the RadioButton is checked or unchecked. When you double click a RadioButton control in design view, an empty event handler for this event is generated. |
Form
s, GroupBox
es, and Panel
s can act as logical groups for RadioButton
s. The RadioButton
s within each group are mutually exclusive to each other, but not to RadioButton
s in different logical groups.
The program in Fig. 14.28 uses RadioButton
s to enable users to select options for a MessageBox
. After selecting the desired attributes, the user presses the Display Button
to display the MessageBox
. A Label
in the lower-left corner shows the result of the MessageBox
(i.e., which Button
the user clicked—Yes, No, Cancel, etc.).
We store the user’s choices in properties IconType
and ButtonType
(declared in lines 13–14). IconType
is of type MessageBoxIcon
, and can have values Asterisk
, Error
, Exclamation
, Hand
, Information
, None
, Question
, Stop
and Warning
. The sample output shows only Error
, Exclamation
, Information
and Question
icons.
ButtonType
is of type MessageBoxButtons
, and can have values AbortRetryIgnore
, OK
, OKCancel
, RetryCancel
, YesNo
and YesNoCancel
. The name indicates the options that are presented to the user in the MessageBox
. The sample output windows show Message-Box
es for all of the MessageBoxButtons
enumeration values.
We created separate GroupBox
es—Button Type and Icon—containing RadioButton
s for the corresponding enumeration options. Only one RadioButton
can be selected from each GroupBox
. When a user clicks the Display Button
, a customized MessageBox
is displayed. A Label
(displayLabel
) shows which Button
the user pressed in the MessageBox
.
The RadioButton
s’ event handler responds to each RadioButton
’s CheckedChanged
event. When a RadioButton
in the Button Type GroupBox
is selected, the event handler (lines 23–54)—which we set for all RadioButton
s in that group—sets the ButtonType
. Similarly, a RadioButton
in the Icon GroupBox
is selected, the event handler (lines 57–98)—which we set for all RadioButton
s in that group—sets the IconType
.
The Click
event handler for displayButton
(lines 101–135) creates a MessageBox
(lines 105–107). The MessageBox
options are specified with the values stored in IconType
and ButtonType
. When the user clicks one of the MessageBox
’s buttons, the result of the message box is returned to the app. This result is a value from the DialogResult
enumeration that contains Abort
, Cancel
, Ignore
, No
, None
, OK
, Retry
or Yes
. The switch
statement in lines 111–134 tests for the result and sets displayLabel.Text
appropriately.