如何获得Radio Buttons的价值?

问题描述 投票:13回答:6

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

o女性

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

谢谢

c# radio-button
6个回答
22
投票

对于Win Forms:

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

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

对于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>

和CS-在一些按钮点击

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

5
投票

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

if(rbMale.Checked)
{

}
else
{

}

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

if(rb1.Checked)
{

}
else if(rb2.Checked)
{

}
else if(rb3.Checked)
{

}

1
投票

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

像这样的东西。

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

1
投票

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

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

当然,您在面板中不能使用其他控件,但如果您只为所有单选按钮设置一个单独的面板(例如在组框内使用子面板或者您希望组织它),则它非常有用你的控件)


1
投票

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

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

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.