C# 如何获得单选按钮的价值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13539051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:56:53  来源:igfitidea点击:

How to get value of Radio Buttons?

c#radio-button

提问by salman

I have a group box contains radio buttons eg.

我有一个包含单选按钮的组框,例如。

oMale

oFemale

o男性

o女性

i want my code to get the selected value of radio button and copy it to string type variable kindly use simple code cause m not very professional

我希望我的代码获取单选按钮的选定值并将其复制到字符串类型变量请使用简单的代码因为我不是很专业

thanks

谢谢

采纳答案by sajanyamaha

For Win Forms :

对于赢形式:

To get the value (assuming that you want the value, not the text) out of a radio button, you get the Checked property:

要从单选按钮中获取值(假设您想要的是值,而不是文本),您需要获取 Checked 属性:

string value = "";
bool isChecked = radioButton1.Checked;
if(isChecked )
  value=radioButton1.Text;
else
  value=radioButton2.Text;

For Web Forms :

对于 Web 表单:

<asp:RadioButtonList ID="rdoPriceRange" runat="server" RepeatLayout="Flow">
    <asp:ListItem Value="Male">Male</asp:ListItem>
    <asp:ListItem Value="Female">Female</asp:ListItem>
</asp:RadioButtonList>

And CS-in some button click

和 CS-in 一些按钮点击

string value=rdoPriceRange.SelectedItem.Value.ToString();

回答by Adil

You need to check one if you have two

如果你有两个,你需要检查一个

if(rbMale.Checked)
{

}
else
{

}

You need to check all the checkboxes if more then two

如果超过两个,您需要检查所有复选框

if(rb1.Checked)
{

}
else if(rb2.Checked)
{

}
else if(rb3.Checked)
{

}

回答by Mark Hall

You can also use a Common Event for your RadioButtons, and you can use the Tagproperty to pass information to your string or you can use the Text Property if you want your string to hold the same value as the Text of your RadioButton.

您还可以为 RadioButton 使用 Common Event,并且您可以使用该Tag属性将信息传递给您的字符串,或者如果您希望您的字符串与 RadioButton 的 Text 保持相同的值,您可以使用 Text 属性。

Something like this.

像这样的东西。

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    if (((RadioButton)sender).Checked == true)
        sex = ((RadioButton)sender).Tag.ToString();
}

回答by Corey Senger

I found that using the Common Event described above works well and you could have the common event set up like this:

我发现使用上面描述的公共事件效果很好,你可以像这样设置公共事件:

private void checkChanged(object sender, EventArgs e)
    {
        foreach (RadioButton r in yourPanel.Controls)
        {
            if (r.Checked)
                textBox.Text = r.Text;
        }
    }

Of course, then you can't have other controls in your panel that you use, but it's useful if you just have a separate panel for all your radio buttons (such as using a sub panel inside a group box or however you prefer to organize your controls)

当然,然后您不能在您使用的面板中拥有其他控件,但是如果您只有一个单独的面板用于所有单选按钮(例如使用组框内的子面板,或者您喜欢组织你的控制)

回答by fatoms

An alterntive is to use an enum and a component class that extends the standard RadioButton.

另一种方法是使用枚举和扩展标准 RadioButton 的组件类。

public enum Genders
{
    Male,
    Female
}

[ToolboxBitmap(typeof(RadioButton))]
public partial class GenderRadioButton : RadioButton
{
    public GenderRadioButton()
    {
        InitializeComponent();
    }

    public GenderRadioButton (IContainer container)
    {
        container.Add(this);

        InitializeComponent();
    }

    public Genders gender{ get; set; }
}

Use a common event handler for the GenderRadioButtons

为 GenderRadioButton 使用通用事件处理程序

private void Gender_CheckedChanged(Object sender, EventArgs e)
{
    if (((RadioButton)sender).Checked)
    {
        //get selected value
        Genders myGender = ((GenderRadioButton)sender).Gender;
        //get the name of the enum value
        string GenderName = Enum.GetName(typeof(Genders ), myGender);
        //do any work required when you change gender
        switch (myGender)
        {
            case Genders.Male:
                break;
            case Genders.Female:
                break;
            default:
                break;
        }
    }
}

回答by KYDronePilot

Windows Forms

Windows 窗体

For cases where there are multiple radio buttons to check, this function is very compact:

对于需要检查多个单选按钮的情况,此功能非常紧凑:

/// <summary>
/// Get the value of the radio button that is checked.
/// </summary>
/// <param name="buttons">The radio buttons to look through</param>
/// <returns>The name of the radio button that is checked</returns>
public static string GetCheckedRadioButton(params RadioButton[] radioButtons)
{
    // Look at each button, returning the text of the one that is checked.
    foreach (RadioButton button in radioButtons)
    {
        if (button.Checked)
            return button.Text;
    }
    return null;
}

回答by Maghalakshmi Saravana

To Get the Value when the radio button is checked

选中单选按钮时获取值

if (rdbtnSN06.IsChecked == true)
{
string RadiobuttonContent =Convert.ToString(rdbtnSN06.Content.ToString());
}
else
{
string RadiobuttonContent =Convert.ToString(rdbtnSN07.Content.ToString());
}