Let's assume this is
System.Windows.Forms
.
Create some data type you need for identification of the action related to the radio button. Let's assume this is a enumeration expressing some option set, but it can be anything:
enum RadioOption {
None, Good, Bad, Fair, Excellent, Perfect,
}
Now, add this data as a
Control.Tag
(which is of the type
System.Object
:
RadioButton rb = new Radio(button);
rb.Tag = RadioOption.Good;
rb.Name = string.Format("&{0}", RadioOption.Good);
rb.Top =
groupBox1.Controls.Add(rb);
rb = new Radio(button);
rb.Tag = RadioOption.Perfect;
rb.Name = string.Format("&{0}", RadioOption.Perfect);
rb.Top =
groupBox1.Controls.Add(rb);
Now, you need to extract this information from a selected radio button:
static RadioOption GetOption(RadioButton radio) {
return (RadioOption)radio.Tag;
}
You should make sure that the method above is only called for the radio buttons "tagged" using the method shown above. You can put any other information in the tag. Define the type you need, depending on how you want to use it.
A very good use of it is the event
RadioButton.CheckedChanged
.
For each of them do the following:
rb.CheckedChanged += (sender, eventArgs) => {
RadioButton aButton = (RadioButton)sender;
RadioOption option = GetOption(sender as RadioButton);
DoSomethingWhenCheckedChanged(aButton.Cheched, option);
};
—SA