How to get value of Radio Buttons?
Asked Answered
R

9

17

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

Reasonable answered 24/11, 2012 at 6:51 Comment(2)
hey @CodingMash he has referred to as 'ratio button' everywhere am really thinking of any such control !Argentite
Its a type definitely, as she gives example of radio buttons, male female one.Elianore
A
28

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:

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

For Web Forms :

<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

string value=rdoPriceRange.SelectedItem.Value.ToString();
Argentite answered 24/11, 2012 at 6:59 Comment(0)
H
9

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

}
Haemorrhage answered 24/11, 2012 at 6:53 Comment(2)
You need to check both if there's two if you don't have a default property set.Dealfish
Bit off topic, but let's program correctly: Why do you check "if (a==true)" isn't it enough for booleans to check "if (a)". Your method seems like "if (a==true) {b=true;} else {b=false;}" while in fact you mean "b = a;"Angus
W
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();
}
Whistler answered 24/11, 2012 at 7:3 Comment(0)
P
2

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)

Pejorative answered 7/7, 2013 at 23:49 Comment(0)
I
2

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;
        }
    }
}
Idem answered 24/7, 2014 at 11:23 Comment(0)
C
1

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());
}
Conservatism answered 19/12, 2019 at 9:50 Comment(0)
I
0

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;
}
Imaginative answered 20/4, 2019 at 14:24 Comment(0)
B
0

You can do easily like bellow,

_employee.Gender = rbtnMale.Checked?rbtnMale.Text:_employee.Gender;
_employee.Gender = rbtnFemale.Checked?rbtnFemale.Text:_employee.Gender;
Beedon answered 3/2, 2021 at 7:51 Comment(0)
A
0

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)
Allottee answered 27/1, 2022 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.