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
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
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();
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)
{
}
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();
}
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)
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;
}
}
}
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());
}
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;
}
You can do easily like bellow,
_employee.Gender = rbtnMale.Checked?rbtnMale.Text:_employee.Gender;
_employee.Gender = rbtnFemale.Checked?rbtnFemale.Text:_employee.Gender;
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)
© 2022 - 2024 — McMap. All rights reserved.