C# Simplified, part 7: Working with WinForm Controls - Working with RadioButtons
(Page 3 of 6 )
With RadioButtons, you can let your users select a single choice at a time, rather than a set of choices as seen in the previous session. Listing 7.3 demonstrates the usage of this control:
Listing 7.3
001: using System;
002: using System.Drawing;
003: using System.Windows.Forms;
004:
005: public class MyRadiobutton: Form
006: {
007: RadioButton rad1 = new RadioButton();
008: RadioButton rad2 = new RadioButton();
009: GroupBox grp = new GroupBox();
010:
011: MyRadiobutton()
012: {
013:
014: rad1.Text = "Employed";
015: rad1.Location = new Point(20,20);
016:
017: rad2.Text = "Unemployed";
018: rad2.Location = new Point(20,40);
019:
020: grp.Location = new Point(20,20);
021: grp.Text = "Your Employment Status";
022: grp.BackColor = Color.Silver;
023:
024: // radio buttons added to group box
025:
026: grp.Controls.Add(this.rad1);
027: grp.Controls.Add(this.rad2);
028:
029: // group box added to the form
030:
031: this.Controls.Add(grp);
032: this.Text = "RadioButton Control Demo";
033: }
034:
035:
036: public static void Main()
037: {
038: MyRadiobutton myrad = new MyRadiobutton();
039: Application.Run(myrad);
040: }
041: }
From the above listing, you can see that RadioButtons are first added to a GroupBox. This is necessary, because otherwise you will not get the functionality of the RadioButton. If you have worked with Visual Basic 6.0, you will be able to infer that GroupBox is similar to Frames. After this process, the GroupBox instance is added to the Form. It is not necessary for you to add each and every RadioButton instance to the form.
Output

Figure 7.3
Next: Working with Labels >>
More C# Articles
More By Anand Narayanaswamy