如何获得Radio Buttons的价值?

时间:2012-11-24 06:51:06

标签: c# radio-button

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

  

o

     

o 女性

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

感谢

8 个答案:

答案 0 :(得分:22)

对于Win表单:

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

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

对于网络表单:

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

并点击某些按钮

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

答案 1 :(得分:5)

如果你有两个

,你需要检查一个
if(rbMale.Checked)
{

}
else
{

}

如果有两个以上的复选框,您需要检查所有复选框

if(rb1.Checked)
{

}
else if(rb2.Checked)
{

}
else if(rb3.Checked)
{

}

答案 2 :(得分:1)

您还可以为RadioButtons使用公共事件,并且可以使用Tag属性将信息传递给字符串,或者如果希望字符串保持与字符串相同的值,则可以使用Text属性。 RadioButton的文字。

像这样。

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

答案 3 :(得分:1)

我发现使用上述公共事件效果很好,您可以设置如下公共事件:

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

当然,您在面板中不能使用其他控件,但如果您只为所有单选按钮设置一个单独的面板(例如使用组框内的子面板或者您喜欢整理你的控件)

答案 4 :(得分:1)

另一种方法是使用扩展标准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; }
}

使用GenderRadioButtons的公共事件处理程序

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;
        }
    }
}

答案 5 :(得分:0)

Windows窗体

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

/// <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;
}

答案 6 :(得分:0)

在选中单选按钮时获取值

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

答案 7 :(得分:0)

你可以像下面一样轻松做到,

_employee.Gender = rbtnMale.Checked?rbtnMale.Text:_employee.Gender;
_employee.Gender = rbtnFemale.Checked?rbtnFemale.Text:_employee.Gender;
相关问题