8.11 Determining the Selected Radio Button

 <  Day Day Up  >  

You want to determine the currently selected radio button in a group of radio buttons as well as be notified when the state of a radio button changes .


Technique

The RadioButton control is similar to the CheckBox control because both contain a Checked property that can translate into an internal Boolean variable for your form. Radio buttons do not have the option to display three states so there is no ThreeState property. The events that allow you to perform an action when the radio button is manipulated include the CheckedChanged and Click events. It's recommended that you handle the CheckedChanged event because the event is fired whenever the Checked property changes, which is either a result of the user clicking the radio button or through programmatic means by changing the Checked property.

Comments

Radio buttons and check boxes are similar controls. Each of these controls can be in an on or off state. Even though the visual appearance of a radio button is either an empty or filled circle, it still uses the idea of being in a checked state similar to a check box.

You use the CheckedChanged event to perform an action when the state of a single radio button changes. If you use a group of radio buttons, this event can fire twice. When a user clicks a radio button in a group, the radio button that is currently checked is set to the unchecked state. This move causes the CheckedChanged event for that radio button to be called, and subsequently, the Checked property for that button is set to false . Next, the CheckedChanged event is fired for the radio button that was clicked because its Checked property has changed to true .

If you need to enumerate the radio buttons within a radio button group, you can access the Controls collection of the control container the radio buttons are located on. You should, however, ensure that you perform a type check on each element because the control container might also contain nonradio buttons. The following code enumerates two groups of radio buttons. Each group is naturally contained within its own control container. As each control is accessed in the Controls collection, its type is checked using the GetType method defined in the Control class, which all .NET controls derive from, and compares the result of that method call using the typeof operator on the RadioButton class. If the logical statement is true , then a radio button is found and can therefore be cast accordingly , as shown in Listing 8.4.

Listing 8.4 Examining Radio Button Checked State
 private void UpdateLabels() {     // enumerate each control in the control container (GroupBox)     foreach( Control ctrl in groupColor.Controls )     {         // determine if radio button         if( ctrl.GetType() == typeof(RadioButton) )         {             // cast control to RadioButton object             RadioButton radio = (RadioButton) ctrl;             if( radio.Checked == true )             {                 labelColor.Text = "Color: " + radio.Text;                 break;             }         }     }     foreach( Control ctrl in groupSize.Controls )     {         if( ctrl.GetType() == typeof(RadioButton) )         {             RadioButton radio = (RadioButton) ctrl;             if( radio.Checked == true )             {                 labelSize.Text = "Size: " + radio.Text;                 break;             }         }     } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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