【问题标题】:How to get value of Radio Buttons?如何获得单选按钮的价值?
【发布时间】:2012-11-12 09:46:08
【问题描述】:

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

o

o女性

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

谢谢

【问题讨论】:

  • 嘿@Co​​dingMash,他在任何地方都提到了“比例按钮”,我真的在考虑任何这样的控制!
  • 绝对是一种类型,她举了单选按钮的例子,男性女性。

标签: c# radio-button


【解决方案1】:

赢取表格:

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

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

对于网络表单:

<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-in 一些按钮点击

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

【讨论】:

    【解决方案2】:

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

    if(rbMale.Checked)
    {
    
    }
    else
    {
    
    }
    

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

    if(rb1.Checked)
    {
    
    }
    else if(rb2.Checked)
    {
    
    }
    else if(rb3.Checked)
    {
    
    }
    

    【讨论】:

    • 如果没有设置默认属性,则需要同时检查是否有两个。
    • 有点题外话了,但是让我们正确编程:为什么要检查“if (a==true)”对于布尔值检查“if (a)”来说还不够。你的方法看起来像 "if (a==true) {b=true;} else {b=false;}" 而实际上你的意思是 "b = a;"
    【解决方案3】:

    您还可以为 RadioButtons 使用公共事件,并且可以使用 Tag 属性将信息传递给您的字符串,或者如果您希望您的字符串与你的 RadioButton。

    类似的东西。

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

    【讨论】:

      【解决方案4】:

      我发现使用上述通用事件效果很好,您可以像这样设置通用事件:

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

      当然,您不能在面板中使用其他控件,但如果您的所有单选按钮只有一个单独的面板(例如在组框内使用子面板,或者您可以更喜欢组织你的控件)

      【讨论】:

        【解决方案5】:

        另一种方法是使用枚举和扩展标准 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; }
        }
        

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

        【讨论】:

          【解决方案6】:

          选中单选按钮时获取值

          if (rdbtnSN06.IsChecked == true)
          {
          string RadiobuttonContent =Convert.ToString(rdbtnSN06.Content.ToString());
          }
          else
          {
          string RadiobuttonContent =Convert.ToString(rdbtnSN07.Content.ToString());
          }
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              【解决方案8】:

              你可以像下面这样轻松地做,

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

              【讨论】:

                【解决方案9】:

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

                用法 您可以将 RadioButtons 添加到 GroupBox、Panel 或 Form 中,然后通过

                获取选中的文本
                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
                

                用法 您可以将 RadioButtons 添加到 GroupBox、Panel 或 Form 中,然后通过

                获取选中的文本
                Dim selectedText1 As String = GetCheckedRadioButtonTextByParent(GroupBox1)
                Dim selectedText2 As String = GetCheckedRadioButtonTextByParent(Panel1)
                Dim selectedText3 As String = GetCheckedRadioButtonTextByParent(Form1)
                

                【讨论】:

                  猜你喜欢
                  • 2011-01-01
                  • 2015-04-21
                  • 2020-07-06
                  • 2017-10-25
                  • 1970-01-01
                  • 2018-06-18
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多