I have aq Windows Forms application with a variable large number of Radio Buttons (50+). I have an event handler for all the Radio Buttons:
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
if(sender == (RadioButton)radioButton1)
{
selectedItem = radioButton1.Text;
}
else
{
selectedItem = radioButton2.Text;
}
}
That works fine for two Radio Buttons. How can I get the text of the selected Radio Button without to use the if clause? Thank you iin advance, Paul
Like this; but you also need to check if it's checked because the event fires when a radio button is de-checked as well, see :
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
//cast and store for future use.
var senderRadioButton = (RadioButton)sender;
//check if sender is checked
if (senderRadioButton.Checked)
selectedItem = senderRadioButton.Text;
//else
}
Make sure you wire all your changed events to this handler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.