grimsan55
grimsan55

Reputation: 265

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

Upvotes: 2

Views: 41728

Answers (6)

Anjana Dinethra
Anjana Dinethra

Reputation: 9

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

Upvotes: 0

Xbold
Xbold

Reputation: 1

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

Upvotes: 0

Christopher Biagtan
Christopher Biagtan

Reputation: 1

  1. Put the checkbox inside the groupbox control.
  2. Loop the control enclosed on the groupbox
  3. 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;
        }
    }
}

Upvotes: 0

Ssc456
Ssc456

Reputation: 176

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

Upvotes: 0

King King
King King

Reputation: 63387

It's so simple to achieve what you want, however it's also so strange:

//We need this to hold the last checked CheckBox
CheckBox lastChecked;
private void chk_Click(object sender, EventArgs e) {
   CheckBox activeCheckBox = sender as CheckBox;
   if(activeCheckBox != lastChecked && lastChecked!=null) lastChecked.Checked = false;
   lastChecked = activeCheckBox.Checked ? activeCheckBox : null;
}

Upvotes: 13

Ssc456
Ssc456

Reputation: 176

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!

Upvotes: 8

Related Questions