none
Checkbox condition not working using Operators && || RRS feed

  • Question

  • my bellow code checkbox condition not working!! where is wrong!! Please help

      

    private void buttonAppend_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Length == 0)
                {
                    MessageBox.Show("Please input user ID");
                    return;
                }

                if ((checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked) || (checkBox2.Checked && !checkBox1.Checked && !checkBox3.Checked) || (checkBox3.Checked && !checkBox2.Checked && !checkBox1.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 1";
                    return;
                }
                else if ((checkBox4.Checked && !checkBox5.Checked && !checkBox6.Checked) || (checkBox5.Checked && !checkBox4.Checked && !checkBox6.Checked) || (checkBox6.Checked && !checkBox4.Checked && !checkBox5.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 2";
                    return;
                }
                else if ((checkBox7.Checked && !checkBox8.Checked && !checkBox9.Checked) || (checkBox8.Checked && !checkBox7.Checked && !checkBox9.Checked) || (checkBox9.Checked && !checkBox7.Checked && !checkBox8.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 3";
                    return;
                }
                else if ((checkBox10.Checked && !checkBox11.Checked && !checkBox12.Checked) || (checkBox11.Checked && !checkBox10.Checked && !checkBox12.Checked) || (checkBox12.Checked && !checkBox10.Checked && !checkBox11.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 4";
                    return;
                }
                else if ((checkBox13.Checked && !checkBox14.Checked && !checkBox15.Checked) || (checkBox14.Checked && !checkBox13.Checked && !checkBox15.Checked) || (checkBox15.Checked && !checkBox13.Checked && !checkBox14.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 5";
                    return;
                }
                else if ((checkBox16.Checked && !checkBox17.Checked && !checkBox18.Checked) || (checkBox17.Checked && !checkBox16.Checked && !checkBox18.Checked) || (checkBox18.Checked && !checkBox16.Checked && !checkBox17.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 6";
                    return;
                }
                else
                {

                //Append
                DataRow fr;
                fr = this.fpdbDataSet1.fplist.Rows.Find(textBox1.Text);
                if (fr == null)
                {
                    DataRow dr = this.fpdbDataSet1.fplist.NewRow();
                    dr[1] = textBox1.Text;
                    dr[2] = textBox2.Text;
                    dr[39] = "N";



                    this.fpdbDataSet1.fplist.Rows.Add(dr);
                    this.fplistTableAdapter1.Update(this.fpdbDataSet1.fplist);



                    }

                }

            }

    • Moved by CoolDadTx Monday, May 9, 2016 2:10 PM Winforms related
    Monday, May 9, 2016 1:31 PM

Answers

  • Hi kafsar,

    You could try the following code:

         Dictionary<string, List<CheckBox>> CheckBoxdict = new Dictionary<string, List<CheckBox>>();
    
            private void InitDict()//load dict
            {
                if (CheckBoxdict.Count == 0)
                {
                    CheckBoxdict.Add("1", new List<CheckBox> { checkBox1, checkBox2, checkBox3 });
                    CheckBoxdict.Add("2", new List<CheckBox> { checkBox4, checkBox5, checkBox6 });
                    CheckBoxdict.Add("3", new List<CheckBox> { checkBox7, checkBox8, checkBox9 });
                    CheckBoxdict.Add("4", new List<CheckBox> { checkBox10, checkBox11, checkBox12 });
                    CheckBoxdict.Add("5", new List<CheckBox> { checkBox13, checkBox14, checkBox15 });
                    CheckBoxdict.Add("6", new List<CheckBox> { checkBox16, checkBox17, checkBox18 });
                }
            }
            private void CheckState()//check checkboxes' state
            {
                if (CheckBoxdict.Count > 0)
                {
                    for (int i = 1; i <= 6; i++)
                    {
                        int count = 0;
                        foreach (CheckBox cb in CheckBoxdict[i.ToString()])
                            if (cb.Checked)
                                count++;
                        if (count <= 1)
                        {
                            MessageBox.Show("step" + i.ToString() + " check failed");
                            return;
                        }
                    }
                }
            }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, May 18, 2016 7:31 AM
    Moderator

All replies

  • This all rather depends on what you mean by "does not work".

    What are you trying to achieve here, what is the desired effect and what is the actual effect.

    I see that you are using checkboxes here in a bit of an odd way, trying to check that only one of a set of checkboxes is checked in every clause. This looks more like you need a few sets of RadioButton controls instead!

    Radiobuttons are described here (Windows Forms) or here (WPF). You may want to look into radio buttons first and consider rewriting your code to use them.

    Monday, May 9, 2016 1:36 PM
  • if ((checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked) || (checkBox2.Checked && !checkBox1.Checked && !checkBox3.Checked) || (checkBox3.Checked && !checkBox2.Checked && !checkBox1.Checked))
                {
                    label6.Visible = true;
                    label6.Text = "Go step 1";
                    return;
                }

    first and second condition worked !

    ((checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked) || (checkBox2.Checked && !checkBox1.Checked && !checkBox3.Checked)

    but third not working!

    || (checkBox3.Checked && !checkBox2.Checked && !checkBox1.Checked))

    Its check if Checkbox1 is checked but Checkbox2 and Checkbox3 not checked then message display.

    or if  Checkbox2 is checked but Checkbox1 and Checkbox3 not checked then message display.

    or if Its check if Checkbox3 is checked but Checkbox1 and Checkbox2 not checked then message display.

     
    • Edited by kafsar Monday, May 9, 2016 1:50 PM
    Monday, May 9, 2016 1:45 PM
  • That is some seriously complex code for something that should be pretty straightforward. As RJP1973 said, you should really be using a radio button if the options are mutually exclusive. It is also unclear how your app is preventing the user from selecting, say, checkbox1, 4 and 7 which means you'd have 3 different results.

    Personally I would recommend that you forego this complex logic altogether. Associate the step information with each box using the Tag property. Then use a radiobutton and groups to make the checkboxes mutually exclusive. Then simplify your code down such that you simply get the selected button in each group, grab the Tag value and then update the UI as normal.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, May 9, 2016 2:10 PM
  • Well, I just created a form, stuck on 3 checkboxes and a button, and copied your code into the button clicked event:

    if ((checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked)

    || (checkBox2.Checked && !checkBox1.Checked && !checkBox3.Checked)

    || (checkBox3.Checked && !checkBox2.Checked && !checkBox1.Checked)) { MessageBox.Show("Yes"); } else { MessageBox.Show("No"); }


    This behaved exactly as expected: The message box showed "Yes" when one of the checkboxes was checked and the other two were not checked. Otherwise the message box showed "No".

    (And I will say again - this really looks like you should be using RadioButton controls!)

    • Edited by RJP1973 Monday, May 9, 2016 2:14 PM
    Monday, May 9, 2016 2:12 PM
  • Hi kafsar,

    You could try the following code:

         Dictionary<string, List<CheckBox>> CheckBoxdict = new Dictionary<string, List<CheckBox>>();
    
            private void InitDict()//load dict
            {
                if (CheckBoxdict.Count == 0)
                {
                    CheckBoxdict.Add("1", new List<CheckBox> { checkBox1, checkBox2, checkBox3 });
                    CheckBoxdict.Add("2", new List<CheckBox> { checkBox4, checkBox5, checkBox6 });
                    CheckBoxdict.Add("3", new List<CheckBox> { checkBox7, checkBox8, checkBox9 });
                    CheckBoxdict.Add("4", new List<CheckBox> { checkBox10, checkBox11, checkBox12 });
                    CheckBoxdict.Add("5", new List<CheckBox> { checkBox13, checkBox14, checkBox15 });
                    CheckBoxdict.Add("6", new List<CheckBox> { checkBox16, checkBox17, checkBox18 });
                }
            }
            private void CheckState()//check checkboxes' state
            {
                if (CheckBoxdict.Count > 0)
                {
                    for (int i = 1; i <= 6; i++)
                    {
                        int count = 0;
                        foreach (CheckBox cb in CheckBoxdict[i.ToString()])
                            if (cb.Checked)
                                count++;
                        if (count <= 1)
                        {
                            MessageBox.Show("step" + i.ToString() + " check failed");
                            return;
                        }
                    }
                }
            }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, May 18, 2016 7:31 AM
    Moderator