Reputation: 126
i designed a desktop application which contain multiple questions and answers were taken with multiple radio button but when new question is answered previous radio button response disappears and new radio button gets checked.Can any one please help me to show all enabled radio buttons in the panel ?
providing the code containing radio button check for all the questions
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);
}
Upvotes: 0
Views: 1448
Reputation: 126
The above asked question worked by wrapping all the Radio buttons using Radio group. Providing the worked code using Radio group.
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);
}
Upvotes: 2
Reputation: 179
Radio buttons are designed for this purpose so that just one of them in a group can be checked at once. In your code, all radio buttons are added to the same container, flowLayoutPanel1, so they all belong to the same group.
To be able to answer each question separately, create a container element for it and put the question and all radio buttons for the answers in this container element. See the answers in this: Grouping Radio Buttons in Windows Forms
Upvotes: 1