'How to get value of Radio Buttons?

I have a group box contains radio buttons eg.

o Male

o Female

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



Solution 1:[1]

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)
{

}

Solution 2:[2]

You can also use a Common Event for your RadioButtons, and you can use the Tag property 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.

Something like this.

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

Solution 3:[3]

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)

Solution 4:[4]

An alterntive is to use an enum and a component class that extends the standard 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

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

Solution 5:[5]

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

Solution 6:[6]

Windows Forms

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

Solution 7:[7]

You can do easily like bellow,

_employee.Gender = rbtnMale.Checked?rbtnMale.Text:_employee.Gender;
_employee.Gender = rbtnFemale.Checked?rbtnFemale.Text:_employee.Gender;

Solution 8:[8]

C# WinForm

public string GetCheckedRadioButtonTextByParent(Control parent)
{
    RadioButton checkedRadioButton = parent.Controls.OfType<RadioButton>.FirstOrDefault(r => r.Checked);
    if (checkedRadioButton is object)
    {
        return checkedRadioButton.Text;
    }

    return "";
}

Usage You can add RadioButtons into GroupBox, Panel or Form then get the checked text by

string selectedText1 = GetCheckedRadioButtonTextByParent(GroupBox1);
string selectedText2 = GetCheckedRadioButtonTextByParent(Panel1);
string selectedText3 = GetCheckedRadioButtonTextByParent(Form1);

VB.NET WinForm

Public Function GetCheckedRadioButtonTextByParent(Byval parent As Control) As String
   Dim checkedRadioButton As RadioButton = parent.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked)
   If checkedRadioButton IsNot Nothing Then
      Return checkedRadioButton.Text
   End If
   Return ""
End Function

Usage You can add RadioButtons into GroupBox, Panel or Form then get the checked text by

Dim selectedText1 As String = GetCheckedRadioButtonTextByParent(GroupBox1)
Dim selectedText2 As String = GetCheckedRadioButtonTextByParent(Panel1)
Dim selectedText3 As String = GetCheckedRadioButtonTextByParent(Form1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mark Hall
Solution 3 Corey Senger
Solution 4
Solution 5 Maghalakshmi Saravana
Solution 6 KYDronePilot
Solution 7 Mafei
Solution 8