多个单选按钮C#的多个问题

时间:2018-11-08 10:20:08

标签: c# radio-button

我设计了一个桌面应用程序,其中包含多个问题,并使用多个单选按钮回答了问题,但是当回答了新问题时,先前的单选按钮响应消失并且新的单选按钮被选中。任何人都可以帮助我显示所有启用的单选按钮在面板中?

提供包含单选按钮的代码以检查所有问题

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

2 个答案:

答案 0 :(得分:1)

单选按钮是为此目的而设计的,因此可以一次检查一组中的一个。在您的代码中,所有单选按钮都添加到了相同的容器flowLayoutPanel1中,因此它们都属于同一组。

要能够分别回答每个问题,请为其创建一个容器元素,然后将问题和答案的所有单选按钮放入此容器元素中。 请参阅以下答案:Grouping Radio Buttons in Windows Forms

答案 1 :(得分:1)

上述问题通过使用“广播”组包装所有“广播”按钮而起作用。 使用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);
}