Section 13.7. CheckBoxes and RadioButtons


13.7. CheckBoxes and RadioButtons

Visual 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.

CheckBoxes

A 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.

Figure 13.25. CheckBox properties and events.

CheckBox properties and events

Description

Common Properties

Checked

Indicates whether the CheckBox is checked (contains a check mark) or unchecked (blank). This property returns a Boolean value.

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 is unclear whether the state should be Checked or Unchecked. For example, in Microsoft Word, when you select a paragraph that contains several character formats, then go to Format > Font, some of the CheckBoxes appear in the Indeterminate state. When CheckState is set to Indeterminate, the CheckBox is usually shaded.

Text

Specifies the text displayed to the right of the CheckBox.

Common Events

CheckedChanged

Generated when the Checked 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.

CheckStateChanged

Generated when the CheckState property changes.


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.

  1  ' Fig. 13.26: FrmCheckBoxTest.vb  2  ' Using CheckBoxes to toggle italic and bold styles.  3  Public Class FrmCheckBoxTest  4     ' toggle the font style between bold and                             5     ' not bold based on the current setting                              6     Private Sub chkBold_CheckedChanged(ByVal sender As System.Object, _  7        ByVal e As System.EventArgs) Handles chkBold.CheckedChanged       8        lblOutput.Font = _                                                9           New Font(lblOutput.Font.Name, lblOutput.Font.Size, _          10              lblOutput.Font.Style Xor FontStyle.Bold)                   11     End Sub ' chkBold_CheckedChanged                                    12 13     ' toggle the font setting between italic and                          14     ' not italic based on the current setting                             15     Private Sub chkItalic_CheckedChanged(ByVal sender As System.Object, _ 16        ByVal e As System.EventArgs) Handles chkItalic.CheckedChanged      17        lblOutput.Font = _                                                 18           New Font(lblOutput.Font.Name, lblOutput.Font.Size, _            19              lblOutput.Font.Style Xor FontStyle.Italic)                   20     End Sub ' chkItalic_CheckedChanged                                    21  End Class ' FrmCheckBoxTest 

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.

RadioButtons

Radio 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

Use RadioButtons when the user should choose only one option in a group.


Look-and-Feel Observation 13.7

Use CheckBoxes when the user should be able to choose multiple options (or no options at all) in a group.


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.

Figure 13.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.


Software Engineering Observation 13.2

Forms, GroupBoxes and Panels act as logical groups for RadioButtons. The RadioButtons within each group are mutually exclusive to each other, but not to those in different logical groups.


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.

  1  ' Fig. 13.28: FrmRadioButtonsTest.vb  2  ' Using RadioButtons to set message window options.  3  Public Class FrmRadioButtonsTest  4     ' create variables that store the user's choice of options  5     Private iconType As MessageBoxIcon       6     Private buttonType As MessageBoxButtons  7  8     ' set button type to OK  9      Private Sub radOk_CheckedChanged(ByVal sender As System.Object, _ 10         ByVal e As System.EventArgs) Handles radOk.CheckedChanged      11         buttonType = MessageBoxButtons.OK                              12      End Sub ' radOk_CheckedChanged                                    13 14      ' set button type to OKCancel 15      Private Sub radOkCancel_CheckedChanged(ByVal sender As System.Object, _ 16         ByVal e As System.EventArgs) Handles radOkCancel.CheckedChanged 17         buttonType = MessageBoxButtons.OKCancel 18      End Sub ' radOkCancel_CheckedChanged 19 20      ' set button type to AbortRetryIgnore 21      Private Sub radAbortRetryIgnore_CheckedChanged( _ 22         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 23         Handles radAbortRetryIgnore.CheckedChanged 24         buttonType = MessageBoxButtons.AbortRetryIgnore 25      End Sub ' radAbortRetryIgnore_CheckedChanged 26 27      ' set button type to YesNoCancel 28      Private Sub radYesNoCancel_CheckedChanged( _ 29         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 30         Handles radYesNoCancel.CheckedChanged 31         buttonType = MessageBoxButtons.YesNoCancel 32      End Sub ' radYesNoCancel_CheckedChanged 33 34      ' set button type to YesNo 35      Private Sub radYesNo_CheckedChanged(ByVal sender As System.Object, _ 36         ByVal e As System.EventArgs) Handles radYesNo.CheckedChanged 37         buttonType = MessageBoxButtons.YesNo 38      End Sub ' radYesNo_CheckedChanged 39 40      ' set button type to RetryCancel 41      Private Sub radRetryCancel_CheckedChanged( _ 42         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 43         Handles radRetryCancel.CheckedChanged 44         buttonType = MessageBoxButtons.RetryCancel 45      End Sub ' radRetryCancel_CheckedChanged 46 47      ' set icon type to Asterisk 48      Private Sub radAsterisk_CheckedChanged(ByVal sender As System.Object, _ 49         ByVal e As System.EventArgs) Handles radAsterisk.CheckedChanged 50         iconType = MessageBoxIcon.Asterisk 51      End Sub ' radAsterisk_CheckedChanged 52 53      ' set icon type to Error 54      Private Sub radError_CheckedChanged(ByVal sender As System.Object, _ 55         ByVal e As System.EventArgs) Handles radError.CheckedChanged 56         iconType = MessageBoxIcon.Error 57      End Sub ' radError_CheckedChanged 58 59      ' set icon type to Exclamation 60      Private Sub radExclamation_CheckedChanged( _ 61         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 62         Handles radExclamation.CheckedChanged 63         iconType = MessageBoxIcon.Exclamation 64      End Sub ' radExclamation_CheckedChanged 65 66      ' set icon type to Hand 67      Private Sub radHand_CheckedChanged(ByVal sender As System.Object, _ 68         ByVal e As System.EventArgs) Handles radHand.CheckedChanged 69         iconType = MessageBoxIcon.Hand 70      End Sub ' radHand_CheckedChanged 71 72      ' set icon type to Information 73      Private Sub radInformation_CheckedChanged( _ 74         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 75         Handles radInformation.CheckedChanged 76         iconType = MessageBoxIcon.Information 77      End Sub ' radInformation_CheckedChanged 78 79      ' set icon type to Question 80      Private Sub radQuestion_CheckedChanged(ByVal sender As System.Object, _ 81         ByVal e As System.EventArgs) Handles radQuestion.CheckedChanged 82         iconType = MessageBoxIcon.Question 83      End Sub ' radQuestion_CheckedChanged 84 85      ' set icon type to Stop 86      Private Sub radStop_CheckedChanged(ByVal sender As System.Object, _ 87         ByVal e As System.EventArgs) Handles radStop.CheckedChanged 88         iconType = MessageBoxIcon.Stop 89      End Sub ' radStop_CheckedChanged 90 91      ' set icon type to Warning 92      Private Sub radWarning_CheckedChanged(ByVal sender As System.Object, _ 93         ByVal e As System.EventArgs) Handles radWarning.CheckedChanged 94         iconType = MessageBoxIcon.Warning 95      End Sub ' radWarning_CheckedChanged 96 97      ' display MessageBox and Button user pressed 98      Private Sub btnDisplay_Click(ByVal sender As System.Object, _ 99         ByVal e As System.EventArgs) Handles btnDisplay.Click 100        ' display MessageBox and store 101        ' the value of the Button that was pressed 102        Dim result As DialogResult = MessageBox.Show( _     103           "This is your Custom MessageBox.", _             104           "Custon MessageBox", buttonType, iconType, 0, 0) 105 106        ' check to see which Button was pressed in the MessageBox 107        ' change text displayed accordingly 108        Select Case result 109           Case Windows.Forms.DialogResult.OK 110              lblDisplay.Text = "OK was pressed" 111           Case Windows.Forms.DialogResult.Cancel 112              lblDisplay.Text = "Cancel was pressed" 113           Case Windows.Forms.DialogResult.Abort 114              lblDisplay.Text = "Abort was pressed" 115           Case Windows.Forms.DialogResult.Retry 116              lblDisplay.Text = "Retry was pressed" 117           Case Windows.Forms.DialogResult.Ignore 118              lblDisplay.Text = "Ignore was pressed" 119           Case Windows.Forms.DialogResult.Yes 120              lblDisplay.Text = "Yes was pressed" 121           Case Windows.Forms.DialogResult.No 122              lblDisplay.Text = "No was pressed" 123        End Select 124     End Sub ' btnDisplay_Click 125  End Class ' FrmRadioButtonsTest 

(a)

(b)

(c) OKCancel button type

(d) OK button type

(e) AbortRetryIgnore button type

(f) YesNoCancel button type

(g) YesNo button type

(h) RetryCancel button type

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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net