How to get value of Radio Buttons?

后端 未结 7 1297
自闭症患者
自闭症患者 2020-12-30 05:05

I have a group box contains radio buttons eg.

o Male

o Female

i want my code to get the sel

相关标签:
7条回答
  • 2020-12-30 05:30

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 05:35

    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)

    0 讨论(0)
  • 2020-12-30 05:36

    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();
    
    0 讨论(0)
  • 2020-12-30 05:40

    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;
    }
    
    0 讨论(0)
  • 2020-12-30 05:45

    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());
    }
    
    0 讨论(0)
  • 2020-12-30 05:50

    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();
    }
    
    0 讨论(0)
提交回复
热议问题