13.7. CheckBoxes and RadioButtonsVisual Basic has two types of state buttons that can be in the on/off or true/false statesCheckBoxes and RadioButtons. Like class Button, classes CheckBox and RadioButton are derived from class ButtonBase. CheckBoxesA 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. Any number of CheckBoxes can be selected at a time. A list of common CheckBox properties and events appears in Fig. 13.25.
The program in Fig. 13.26 allows the user to select CheckBoxes 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 CheckBoxes are selected, the font style is set to bold and italic. Initially, neither CheckBox is checked. Figure 13.26. Using CheckBoxes to change font styles.
The chkBold has its Text property set to Bold. The chkItalic has its Text property set to Italic. The Text property of lblOutput is set to Watch the fontstyle change. After creating the controls, we define their event handlers. Double clicking the CheckBoxes at design time creates empty CheckedChanged event handlers. To change the font style on a Label, you must set its Font property to a new Font object (lines 810 and 1719). The Font constructor that we use here takes the font name, size and style as arguments. The first two arguments lblOutput.Font.Name and lblOutput.Font.Sizeuse lblOutput'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 operatorsoperators that perform manipulations on bits of information. All data is represented in the computer as combinations of 0s and 1s. Each 0 or 1 represents a bit. FontStyle has a System.FlagAttribute, meaning that the FontStyle bit values 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 these various font styles using either the Or operator or the Xor operator. When the 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 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 Or operator to combine the styles, we obtain the bits 11. 01 = Bold 10 = Italic -- 11 = Bold and Italic The Or operator helps create style combinations. However, what happens if we want to undo a style combination, as we did in Fig. 13.26? The Xor operator enables us to combine styles and to undo existing style settings. When Xor 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 Xor 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 Xor on both styles, we obtain the bits 11. 01 = Bold 10 = Italic -- 11 = Bold and Italic Now suppose that we would 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 Xor 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 different FontStyle values (Bold, Italic, Regular, Strikeout and Underline), resulting in 16 different FontStyle combinations. Using bitwise operators to combine font styles greatly reduces the amount of code required to check all possible font combinations. In Fig. 13.26, we need to set the FontStyle so that the text appears in bold if it was not bold originally, and vice versa. Note that line 10 uses the Xor operator to do this. If lblOutput.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 19. If we did not use bitwise operators to compound FontStyle elements, we would have to test for the current style and change it accordingly. For example, in event handler chkBold_CheckChanged, 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. RadioButtonsRadio buttons (defined with class RadioButton) are similar to CheckBoxes in that they also have two statesselected and not selected (also called deselected). However, RadioButtons 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, RadioButtons 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). Look-and-Feel Observation 13.6
Look-and-Feel Observation 13.7
All RadioButtons added to a container are part of the same group. To separate them into several groups, the RadioButtons must be added to GroupBoxes or Panels. The common properties and a common event of class RadioButton are listed in Fig. 13.27.
Software Engineering Observation 13.2
The program in Fig. 13.28 uses RadioButtons 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 clickedYes, No, Cancel, etc.). Figure 13.28. Using RadioButtons to set message-window options.
To store the user's choices, we create and initialize the iconType and buttonType objects (lines 56). Object iconType is of type MessageBoxIcon, and can have values Asterisk, Error, Exclamation, Hand, Information, None, Question, Stop and Warning. The sample output shows only the Error, Exclamation, Information and Question icons. Object 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 MessageBoxes for all of the MessageBoxButtons enumeration values. We created two GroupBoxes, one for each set of enumeration values. The GroupBox captions are Button Type and Icon. The GroupBoxes contain RadioButtons for the corresponding enumeration options, and the RadioButtons' Text properties are set appropriately. Because the RadioButtons are grouped, only one RadioButton can be selected from each GroupBox. There is also a Button (btnDisplay) labeled Display. When a user clicks this Button, a customized MessageBox is displayed. A Label (lblDisplay) displays which Button the user pressed in the MessageBox. The event handler for each RadioButton handles the CheckedChanged event for that RadioButton. When a RadioButton contained in the Button Type GroupBox is checked, the checked RadioButton's corresponding event handler sets buttonType to the appropriate value. Lines 945 contain the event handling for these RadioButtons. Similarly, when the user checks the RadioButtons belonging to the Icon GroupBox, the event handlers associated with these events (lines 4895) set iconType to its corresponding value. The btnDisplay_Click event handler (lines 98124) creates a MessageBox (lines 102104) with options specified by the values of iconType and buttonType. When the user clicks one of the MessageBox's buttons, the result of the message box is returned to the application. This result is a value from the DialogResult enumeration that contains Abort, Cancel, Ignore, No, None, OK, Retry or Yes. The Select Case statement in lines 108123 tests for the result and sets lblDisplay.Text appropriately. |