Whats wrong with the states of the check boxes?

Ivan Gallardo 1 Reputation point
2022-03-21T13:21:07.067+00:00

c# with windows forms.

It seems that there's some event that's sticking in the behavior, and I don't understand which and how to disable it.

I have checkboxes that should show three labels and a panel in common with other checkboxes and show its own. Once you uncheck, it should verify if there's another checkbox checked and if it's true, should keep the labels and the panel visible and hide it own label, but the last unselected should hide the panel and the three labels with it own.

The code I have.

public void chBox()
        {

            foreach (CheckBox boxes in panel3.Controls)
            {
                if (boxes.Checked == true)

                    panel4.Visible = true;
                else
                    panel4.Visible = false;
            }

        }
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox1.Checked)
            {
                chBox();
                label4.Visible = true;
            }
            else
            {
                label4.Visible = false;
            }

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox2.Checked)
            {
                chBox();
                label6.Visible = true;
            }
            else
            {
                label6.Visible = false;
            }
        }

Thanks a lot if somebody can give a Hint.

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-03-21T13:59:19.207+00:00

    Check if this works:

    public void chBox()
    {
       panel4.Visible = false;
       foreach (CheckBox box in panel3.Controls)
       {
          if (box.Checked) 
          {
             panel4.Visible = true;
             break;
          }
       }
    }
    
    0 comments No comments

  2. Ivan Gallardo 1 Reputation point
    2022-03-21T15:08:05.357+00:00

    Sorry but I just solved with a counter, I did try with a break but I couldn't make it.

    The counter is between 0 and 1, and then I called the method in every click which adds their own label.

    Thanks a lot.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.