'Only one checkbox to be selected
I would like to only have single checkbox selected at a time. My program reads from a textfile and creates checkboxes according to how many "answers" there are in the textfile.
Does anyone know what is wrong with the code?
public partial class Form1 : Form
{
string temp = "questions.txt";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(temp);
string line = "";
List<string> enLista = new List<string>();
while ((line = sr.ReadLine()) != null)
{
string[] myarray = line.Split('\r');
enLista.Add(myarray[0]);
}
sr.Close();
for (int i = 0; i < enLista.Count; i++)
{
if (enLista[i].Contains("?"))
{
Label lbl = new Label();
lbl.Text = enLista[i].ToString();
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
}
else if (enLista[i] == "")
{
}
else
{
CheckBox chk = new CheckBox();
chk.Text = enLista[i].ToString();
flowLayoutPanel1.Controls.Add(chk);
chk.Click += chk_Click;
}
}
}
private void chk_Click(object sender, EventArgs e)
{
CheckBox activeCheckBox = sender as CheckBox;
foreach (Control c in Controls)
{
CheckBox checkBox = c as CheckBox;
if (checkBox != null)
{
if (!checkBox.Equals(activeCheckBox))
{ checkBox.Checked = !activeCheckBox.Checked; }
else
{ checkBox.Checked = true; }
}
}
}
}
Solution 1:[1]
I think you are looking for Radio Buttons.
If you are insistent on using checkboxes then change your event to CheckedChanged as this will be more accurate. Check boxes can unfortunately be clicked without checking themselves!
Solution 2:[2]
Ok this should do what you want to do, either onClick or on CheckChanged but the answer is from CheckChanged.
Put this in the chk_CheckChanged event and add the chk_CheckChanged event to each Checkbox you add.
CheckBox tmp = (CheckBox)sender;
foreach (CheckBox c in flowLayoutPanel1.Controls)
{
c.CheckedChanged -= chk_CheckedChanged;
c.Checked = false;
}
tmp.Checked = true;
foreach (CheckBox c in flowLayoutPanel1.Controls)
{
c.CheckedChanged += chk_CheckedChanged;
}
Solution 3:[3]
- Put the checkbox inside the groupbox control.
- Loop the control enclosed on the groupbox
- Find the checkbox name in the loop, if not match make the checkbox unchecked else checked the box.
See Sample code:
//Event CheckedChanged of checkbox:
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
if (cb.CheckState == CheckState.Checked)
{
checkboxSelect(cb.Name);
}
}
//Function that will check the state of all checkbox inside the groupbox
private void checkboxSelect(string selectedCB)
{
foreach (Control ctrl in groupBox1.Controls)
{
if (ctrl.Name != selectedCB)
{
CheckBox cb = (CheckBox)ctrl;
cb.Checked = false;
}
}
}
Solution 4:[4]
Try using an external Bool variable to know when the CheckBox was changed "automatically"
CheckBox checkBox1;
CheckBox checkBox2;
Bool changed = false; //This one
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!changed)
{
if (checkBox2.Checked)
{
changed = true;
checkBox2.Checked = false;
}
}
else
{
changed = false;
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (!changed)
{
if (checkBox1.Checked)
{
changed = true;
checkBox1.Checked = false;
}
}
else
{
changed = false;
}
}
Solution 5:[5]
Add a checkedListBox and try this
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = checkedListBox1.SelectedIndex;
int count = checkedListBox1.Items.Count;
for(int i = 0; i < count; i++)
{
if(index != i)
{
checkedListBox1.SetItemChecked(i, false);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ssc456 |
| Solution 2 | Ssc456 |
| Solution 3 | Tom Sabel |
| Solution 4 | |
| Solution 5 | Anjana Dinethra |
