none
Problem with checking and uncheck checkboxes RRS feed

  • Question

  • Hey!

    Im doing an app that contains some checkboxes. I want to make that if you check the first checkbox. Then the other checkboxes get checked to. But if you check out someone of the other checkboxes then, the first one will be unchecked because not everyone are checked! but if you check the unchecked one again then the first one will be checked again! Sorry for my bad english..


    void checkboxeAll_CheckedChanged(object sender, EventArgs e)
            {
                //for(int i = 0; i < checkBoxesList.Count(); i++)
                //{
                //    if (checkBoxesList[i].Checked == true)
                //    {
                //        return;
                //    }
                    
                //}
    
                if (checkboxeAll.Checked == true && test2 == false)
                {
                    for (int i = 0; i < checkBoxesList.Count(); i++)
                    {
    
                        checkBoxesList[i].Checked = true;
                        //MessageBox.Show(checkBoxesList[i].Checked.ToString());
                        ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
    
    
                    }
                    test = true;
                }
                //if (checkboxeAll.Checked == true && test2 == true)
                //{
                //    for (int i = 0; i < checkBoxesList.Count(); i++)
                //    {
                //        break;
                //        // checkBoxesList[i].Checked = true;
                //        //MessageBox.Show(checkBoxesList[i].Checked.ToString());
                //        //ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
    
    
                //    }
                //    test = true;
                //}
                //test = true;
                //else if (checkboxeAll.Checked == false)
                //{
                //    for (int i = 0; i < checkBoxesList.Count; i++)
                //    {
                //        checkBoxesList[i].Checked = false;
                //    }
                //}
            }
    
            void textBoxes2_TextChanged(object sender, EventArgs e)
            {
                if (checkboxeAll.Checked == true)
                {
                    if (TaBort == true)
                    {
                        for (int i = 0; i < ÄndraTillLista.Count(); i++)
                        {
                            if (checkBoxesList[i].Checked == true)
                            {
                                ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
                            }
                        }
                    }
                }
                else
                {
                    if (TaBort == true)
                    {
                        for (int i = 0; i < ÄndraTillLista.Count(); i++)
                        {
                            if (checkBoxesList[i].Checked == true)
                            {
                                ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
                            }
                            if (checkBoxesList[i].Checked == false)
                            {
                                
                            }
    
                        }
                    }
                }
    
                if (ÄndraTillLista[0].Text == "")
                {
                    checkBoxesList[0].Checked = false;
                }
                else
                {
                    checkBoxesList[0].Checked = true;
                }
            }
    
            void checkboxes_CheckedChanged(object sender, EventArgs e)
            {
                for (int i = 0; i < checkBoxesList.Count(); i++)
                {
    
    
                    if (checkBoxesList[i].Checked == false && test == true)
                    {
                        checkboxeAll.Checked = false;
                    }
                    //else if (checkBoxesList[i].Checked == true && test == true)
                    //{
                    //    test2 = true;
                    //    checkboxeAll.Checked = true;
    
                    //}
    
                }

    Have tried many ways but cant get it to work..

    • Moved by CoolDadTx Wednesday, November 20, 2019 2:35 PM Winforms related
    Tuesday, November 19, 2019 11:41 AM

Answers

  • Then in you can store dynamically created CheckBoxes in list or container. In my sample at OnCheckBoxAllCheckedChanged you then set all CheckBoxes in list/container to Checked or not. When you create dynamic CheckBox set it CheckedChanged to be handled by OnCheckBoxItemCheckedChanged and in that handler set all CheckBoxChecked based on CheckBoxes in list/container.

    For example if dynamic checkboxes would be inside the GroupBox, then event handlers could look like this:

    private bool handleItemCheck = false;
    private bool handleAllCheck = false;
    
    private void OnCheckBoxAllCheckedChanged(object sender, EventArgs e)
    {
        if (handleItemCheck)
            return;
    
        handleAllCheck = true;
        groupBoxItems.Controls.OfType<CheckBox>().ToList().ForEach(checkBox => checkBox.Checked = checkBoxAll.Checked);
        handleAllCheck = false;
    }
    
    private void OnCheckBoxItemCheckedChanged(object sender, EventArgs e)
    {
        if (handleAllCheck)
            return;
    
        handleItemCheck = true;
    
        checkBoxAll.Checked = groupBoxItems.Controls.OfType<CheckBox>().All(checkBox => checkBox.Checked);
    
        handleItemCheck = false;
    }

    • Marked as answer by Carlo Goretti Wednesday, November 20, 2019 1:06 PM
    Wednesday, November 20, 2019 5:45 AM

All replies

  • Here is quick sample form that implements behavior with three Item checkboxes.

    Form

    The code is

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private bool handleItemCheck = false;
        private bool handleAllCheck = false;
    
        private void OnCheckBoxAllCheckedChanged(object sender, EventArgs e)
        {
            if (handleItemCheck)
                return;
    
            handleAllCheck = true;
            checkBoxItem1.Checked = checkBoxAll.Checked;
            checkBoxItem2.Checked = checkBoxAll.Checked;
            checkBoxItem3.Checked = checkBoxAll.Checked;
            handleAllCheck = false;
        }
    
        private void OnCheckBoxItemCheckedChanged(object sender, EventArgs e)
        {
            if (handleAllCheck)
                return;
    
            handleItemCheck = true;
    
            checkBoxAll.Checked = checkBoxItem1.Checked && checkBoxItem2.Checked && checkBoxItem3.Checked;
    
            handleItemCheck = false;
        }
    }

    Tuesday, November 19, 2019 2:41 PM
  • Any ideas how i can use that in my code..? Im using some dynamically checkboxes
    Tuesday, November 19, 2019 7:23 PM
  • Another possibility would be to use a custom CheckedListBox. If interested I will post the code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 20, 2019 12:36 AM
    Moderator
  • Then in you can store dynamically created CheckBoxes in list or container. In my sample at OnCheckBoxAllCheckedChanged you then set all CheckBoxes in list/container to Checked or not. When you create dynamic CheckBox set it CheckedChanged to be handled by OnCheckBoxItemCheckedChanged and in that handler set all CheckBoxChecked based on CheckBoxes in list/container.

    For example if dynamic checkboxes would be inside the GroupBox, then event handlers could look like this:

    private bool handleItemCheck = false;
    private bool handleAllCheck = false;
    
    private void OnCheckBoxAllCheckedChanged(object sender, EventArgs e)
    {
        if (handleItemCheck)
            return;
    
        handleAllCheck = true;
        groupBoxItems.Controls.OfType<CheckBox>().ToList().ForEach(checkBox => checkBox.Checked = checkBoxAll.Checked);
        handleAllCheck = false;
    }
    
    private void OnCheckBoxItemCheckedChanged(object sender, EventArgs e)
    {
        if (handleAllCheck)
            return;
    
        handleItemCheck = true;
    
        checkBoxAll.Checked = groupBoxItems.Controls.OfType<CheckBox>().All(checkBox => checkBox.Checked);
    
        handleItemCheck = false;
    }

    • Marked as answer by Carlo Goretti Wednesday, November 20, 2019 1:06 PM
    Wednesday, November 20, 2019 5:45 AM
  •         void checkboxeAll_CheckedChanged(object sender, EventArgs e)
            {
                //for(int i = 0; i < checkBoxesList.Count(); i++)
                //{
                //    if (checkBoxesList[i].Checked == true)
                //    {
                //        return;
                //    }
    
                //}
                if (handleItemCheck)
                    return;
    
    
                if (checkboxeAll.Checked == true && test2 == false)
                {
                    //handleAllCheck = true;
                    for (int i = 0; i < checkBoxesList.Count(); i++)
                    {
    
                        checkBoxesList[i].Checked = true;
                        //MessageBox.Show(checkBoxesList[i].Checked.ToString());
                        ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
    
    
                    }
                    handleAllCheck = false;
                    test = true;
                }
                //if (checkboxeAll.Checked == true && test2 == true)
                //{
                //    for (int i = 0; i < checkBoxesList.Count(); i++)
                //    {
                //        break;
                //        // checkBoxesList[i].Checked = true;
                //        //MessageBox.Show(checkBoxesList[i].Checked.ToString());
                //        //ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
    
    
                //    }
                //    test = true;
                //}
                //test = true;
                //else if (checkboxeAll.Checked == false)
                //{
                //    for (int i = 0; i < checkBoxesList.Count; i++)
                //    {
                //        checkBoxesList[i].Checked = false;
                //    }
                //}
            }
    
            void textBoxes2_TextChanged(object sender, EventArgs e)
            {
                if (handleAllCheck)
                    return;
    
                if (checkboxeAll.Checked == true)
                {
                    if (TaBort == true)
                    {
                        for (int i = 0; i < ÄndraTillLista.Count(); i++)
                        {
                            if (checkBoxesList[i].Checked == true)
                            {
                                ÄndraTillLista[i].Text = ÄndraTillLista[0].Text;
                            }
                        }
                    }
                }
                else
                {
                    if (TaBort == true)
                    {
    
                        for (int i = 0; i < ÄndraTillLista.Count(); i++)
                        {
                            handleItemCheck = true;
                            checkboxeAll.Checked = checkBoxesList[i].Checked;
                            handleItemCheck = false;
                        }
                    }
                }
    Tryied this but it didnt work with your code.. Is it something im doing wrong? @MasaSam
    Wednesday, November 20, 2019 5:46 AM
  • Hi Carlo,

    Thank you for posting here.

    Could you please tell us what ÄndraTillLista is?

    It seems to be indispensable for your code, we need to know what it is to reproduce your problem.

    Looking forward to your reply.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 20, 2019 7:23 AM
  • How does it not work? And how you want it to work expecially in text change?

    Based on just viewing your code there are multiple reasons, starting that TaBort could be false.

    Wednesday, November 20, 2019 7:23 AM
  • I got it to work!
    Wednesday, November 20, 2019 1:06 PM