[英]How can I get the text of the selected Radio Button without to use the if clause?
I have aq Windows Forms application with a variable large number of Radio Buttons (50+). 我有一个带有大量单选按钮(50+)的Windows Forms应用程序。 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? 如何不使用if子句而获得所选单选按钮的文本? 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. 确保将所有更改的事件连接到此处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.