There are two groups of radio buttons presented in the window shown in Figure 19-3. If a radio button is chosen in RadioGroup1, the event handler associated with that button is immediately actuated. When this method is chosen , the user has no time to recover from a mistake (picking the wrong radio button).
The RadioGroup2 scheme allows the user to pick a button, change her mind and pick a different button, then invoke the proper event handler when the Go! button is clicked.
Since each group of three radio buttons was placed onto a group box, the three buttons act like radio buttons are supposed to: Only one of the group can be selected. If the programmer does not create a group box first (and set each radio button onto that group box), the radio buttons all interact independently, thus defeating the purpose of a list of buttons.
The radio button code is shown below:
Form3
Form3.cs: SC0500: #region Using directives 7 lines, same as SC0002 SC0008] SC0508: #endregion SC0509: namespace StandardControls SC0510: { SC0511: partial class Form3 : Form SC0512: { SC0513: public Form3() SC0514: { SC0515: InitializeComponent(); SC0516: } //-----------------------------------------------------------------------------------------// SC0520: private void radioButton2_CheckedChanged(object sender, EventArgs e) SC0521: { // radioButton2. SC0522: MessageBox.Show("radioButton2 picked."); SC0522B: return; SC0523: // Close(); SC0524: } //-----------------------------------------------------------------------------------------// SC0530: private void radioButton3_CheckedChanged(object sender, EventArgs e) SC0531: { // radioButton3. SC0532: MessageBox.Show("radioButton3 picked."); SC0533: Close(); SC0534: } //-----------------------------------------------------------------------------------------// SC0540: private void radioButton4_CheckedChanged(object sender, EventArgs e) SC0541: { // radioButton4. SC0542: MessageBox.Show("radioButton4 picked."); SC0543: Close(); SC0544: } //-----------------------------------------------------------------------------------------// SC0550: private void button1_Click(object sender, EventArgs e) SC0551: { // radioGroup2 Go!. SC0552: if (radioButton5.Checked) MessageBox.Show("radioButton5 picked."); SC0553: else if (radioButton6.Checked) MessageBox.Show("radioButton6 picked."); SC0554: else if (radioButton7.Checked) MessageBox.Show("radioButton7 picked."); SC0555: Close(); SC0556: } //-----------------------------------------------------------------------------------------// SC0560: private void button2_Click(object sender, EventArgs e) SC0561: { // Quit. SC0562: Close(); SC0563: } SC0564: } SC0565: } //========================================================================================// Form3.Designer.cs: SC0600: namespace StandardControls SC0601: { [1 label, 2 groupBoxes, 6 radioButtons, 2 buttons] SC0733: }
The return statement in line SC0522B was placed there to demonstrate the difference between return and Close(): return (line SC0522B) returns to Figure 19-3, and Close() (line SC0523) reverts to the main window, which is shown in Figure 19-1 (the window that created Figure 19-3). We seldom exit a control event handler like this with a return unless it is a text-checking handler.