简体   繁体   English

如何不使用if子句而获得所选单选按钮的文本?

[英]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.

相关问题 如何获得单选按钮所选项目的文本? - How do I get the text of the radio button selected item? 如何在数据列表中获取选定的单选按钮文本? - How to get selected radio button text in datalist ? 如何使用jquery获取选定的asp单选按钮? - how can i get the selected asp radio button using jquery? 如何在不使用GroupBox的情况下为单选按钮提供值并确定选定的单选按钮 - How can I give a radio button a value and determine the selected radio button without using a GroupBox 如何从单选按钮列表和复选框列表中获取选定的值? - How can I get selected values from radio button list and check box list? 如何根据选定的单选按钮值传递单选按钮值? - How can i pass radio button values based on the selected one? 如何在不删除文本值的情况下“禁用”单选按钮,以及是否选中它 - How can I "disable" a radio button, without removing the text value, and whether it's checked or not 如何以一种形式使用多个单选按钮和多个组,以便每行和每列只能选择 1 个按钮? - How would I use multiple radio buttons and multiple groups in one form so that only 1 button per row and column can be selected? 重新打开表单时不使用C#时如何获取选定的组合框项目和按钮上的文本 - How to get selected combobox items and text on button when I reopen the form without using serialization in C# 如何在不选择的情况下将单选按钮切换为是或否 - How can I switch radio button to yes or no without selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM