带有多个单选按钮的多个问题c#
Posted
技术标签:
【中文标题】带有多个单选按钮的多个问题c#【英文标题】:multiple questions with multiple radio button c# 【发布时间】:2019-04-11 20:10:22 【问题描述】:我设计了一个桌面应用程序,其中包含多个问题,并且使用多个单选按钮获取答案,但是当回答新问题时,以前的单选按钮响应消失并且新的单选按钮被选中。谁能帮我显示所有启用的单选按钮在面板中?
提供包含单选按钮检查所有问题的代码
while (dr13.Read())
String ss = (dr13["ans"]).ToString();
Console.WriteLine(ss);
String comme = (dr13["comment"]).ToString();
Console.WriteLine(comme);
String fridd = (dr13["frid"]).ToString();
Console.WriteLine(fridd);
RadioButton rb = new RadioButton();
rb.Width = 130;
rb.Text = "Satisfactory";
rb.ForeColor = Color.White;
if (ss == "Satisfactory")
rb.Checked = true;
rb.CheckedChanged += marpolradiosatis;
rb.Tag = fridd;
flowLayoutPanel1.Controls.Add(rb);
RadioButton rb1 = new RadioButton();
rb1.Text = "Not satisfactory";
rb1.Width = 130;
rb1.ForeColor = Color.White;
if (ss == "Not satisfactory")
rb1.Checked = true;
rb1.CheckedChanged += marpolradionot;
rb1.Tag = fridd;
flowLayoutPanel1.Controls.Add(rb1);
RadioButton rb2 = new RadioButton();
rb2.Text = "Need improvement";
rb2.Width = 160;
rb2.ForeColor = Color.White;
if (ss == "Need improvement")
rb2.Checked = true;
rb2.CheckedChanged += marpolradioneed;
rb2.Tag = fridd;
flowLayoutPanel1.Controls.Add(rb2);
RadioButton rb3 = new RadioButton();
rb3.Text = "NA";
rb3.Width = 130;
rb3.ForeColor = Color.White;
if (ss == "NA")
rb3.Checked = true;
rb3.CheckedChanged += marpolradiona;
rb3.Tag = fridd;
flowLayoutPanel1.Controls.Add(rb3);
【问题讨论】:
你的问题是什么? 点击另一个问题下的新单选按钮时,上一个单选按钮响应消失,如何避免这种情况? 【参考方案1】:上述问题通过使用 Radio 组包装所有单选按钮来工作。 使用 Radio 组提供工作代码。
while (dr13.Read())
String ss = (dr13["ans"]).ToString();
Console.WriteLine(ss);
String comme = (dr13["comment"]).ToString();
Console.WriteLine(comme);
String fridd = (dr13["frid"]).ToString();
GroupBox gb = new GroupBox();
gb.Width = 700;
gb.Height = 50;
RadioButton rb = new RadioButton();
rb.Width = 130;
rb.Text = "Satisfactory";
rb.ForeColor = Color.White;
rb.Name = fridd;
if (ss == "Satisfactory")
rb.Checked = true;
RadioButton rb1 = new RadioButton();
rb1.Text = "Not satisfactory";
rb1.Width = 130;
rb1.ForeColor = Color.White;
rb1.Name = fridd;
if (ss == "Not satisfactory")
rb1.Checked = true;
RadioButton rb2 = new RadioButton();
rb2.Text = "Need improvement";
rb2.Width = 160;
rb2.ForeColor = Color.White;
rb2.Name = fridd;
if (ss == "Need improvement")
rb2.Checked = true;
RadioButton rb3 = new RadioButton();
rb3.Text = "NA";
rb3.Width = 130;
rb3.ForeColor = Color.White;
rb.Name = fridd;
if (ss == "NA")
rb3.Checked = true;
gb.Controls.Add(rb);
gb.Controls.Add(rb1);
gb.Controls.Add(rb2);
gb.Controls.Add(rb3);
this.Controls.Add(gb);
flowLayoutPanel1.Controls.Add(gb);
【讨论】:
【参考方案2】:单选按钮专为此目的而设计,以便一次只能检查一组中的一个。在您的代码中,所有单选按钮都添加到同一个容器 flowLayoutPanel1,因此它们都属于同一个组。
为了能够分别回答每个问题,请为其创建一个容器元素,并将问题和答案的所有单选按钮放入此容器元素中。 请参阅此处的答案:Grouping Radio Buttons in Windows Forms
【讨论】:
以上是关于带有多个单选按钮的多个问题c#的主要内容,如果未能解决你的问题,请参考以下文章
UIButton 状态在滚动带有多个部分的 tableview 时发生变化 - Swift
UIButton状态在用多个部分滚动tableview时改变 - Swift