locked
Dynamic Checkboxes with a eventhandler RRS feed

  • Question

  • Hey!

    I need some help..
    I have created two panels.. One with dynamic checkboxes and one with other dynamic panels in it..

    Im trying to create that if dynamic checkbox.check == true. Then it creates a dynamic panel in the other panel.
    The problem comes when the first checkbox and third checkbox are checked. Then it gets a space between them..
    I want them after each other..


        

    public void HittaAllaJobb() { lstChckBox = new List<CheckBox>(); for (int i = 0; i < test.Count(); i++) { box = new CheckBox(); box.Name = i.ToString(); box.Font = new Font(box.Font.FontFamily, 8); box.AutoSize = true; box.Text = test[i].Name; box.Location = new Point(0, (i * 25)); box.CheckedChanged += new EventHandler(Checkbox_Click); lstChckBox.Add(box); CheckboxPanel.Controls.Add(box); } } void Checkbox_Click(object sender, EventArgs e) { for (int i = 0; i < lstChckBox.Count(); i++) { if (lstChckBox[i].Checked == true) { panel = new Panel(); panel.Name = "VirituellPanel " + i; panel.AutoSize = true; panel.Size = new Size(249, 80); panel.Location = new Point(0, (i * 100)); panel.BorderStyle = BorderStyle.FixedSingle; SchemalaggareInfoPanel.Controls.Add(panel); lblRubrik_1 = new Label(); lblRubrik_1.Name = "VirituellLblRubrik1 " + i; lblRubrik_1.Font = new Font(box.Font.FontFamily, 8); lblRubrik_1.AutoSize = true; lblRubrik_1.Text = "Nästa körning är:"; lblRubrik_1.Location = new Point(0, 0); panel.Controls.Add(lblRubrik_1); lblRubrik_2 = new Label(); lblRubrik_2.Name = "VirituellLblRubrik2 " + i; lblRubrik_2.Font = new Font(box.Font.FontFamily, 8); lblRubrik_2.AutoSize = true; lblRubrik_2.Text = "Senaste körning:"; lblRubrik_2.Location = new Point(0, 25); panel.Controls.Add(lblRubrik_2); lblRubrik_3 = new Label(); lblRubrik_3.Name = "VirituellLblRubrik3 " + i; lblRubrik_3.Font = new Font(box.Font.FontFamily, 8); lblRubrik_3.AutoSize = true; lblRubrik_3.Text = "Status:"; lblRubrik_3.Location = new Point(0, 50); panel.Controls.Add(lblRubrik_3); var task = ts.GetTask(test[i].Name); string StatusText = ""; lblVärde_1 = new Label(); lblVärde_1.Name = "VirituellLblVärde1 " + i; lblVärde_1.Font = new Font(box.Font.FontFamily, 8); lblVärde_1.AutoSize = true; lblVärde_1.Text = task.NextRunTime.ToString(); lblVärde_1.Location = new Point(lblRubrik_1.Location.X + lblRubrik_1.Size.Width, lblRubrik_1.Location.Y); panel.Controls.Add(lblVärde_1); lblVärde_2 = new Label(); lblVärde_2.Name = "VirituellLblVärde2 " + i; lblVärde_2.Font = new Font(box.Font.FontFamily, 8); lblVärde_2.AutoSize = true; lblVärde_2.Text = task.LastRunTime.ToString(); lblVärde_2.Location = new Point(lblRubrik_2.Location.X + lblRubrik_2.Size.Width, lblRubrik_2.Location.Y); panel.Controls.Add(lblVärde_2); if (task.IsActive == true) { StatusText = "Aktiverad"; } else { StatusText = "Inaktiverad"; } lblVärde_3 = new Label(); lblVärde_3.Name = "VirituellLblVärde3 " + i; lblVärde_3.Font = new Font(box.Font.FontFamily, 8); lblVärde_3.AutoSize = true; lblVärde_3.Text = StatusText; lblVärde_3.Location = new Point(lblRubrik_3.Location.X + lblRubrik_3.Size.Width, lblRubrik_3.Location.Y); panel.Controls.Add(lblVärde_3); pictureBox = new PictureBox(); pictureBox.Size = new Size(20, 20); pictureBox.Location = new Point(lblVärde_3.Location.X + lblVärde_3.Size.Width + 5, lblVärde_3.Location.Y - 3); if (task.IsActive == true) { pictureBox.Image = Resources.GreenPicture; } else { pictureBox.Image = Resources.Gul; } panel.Controls.Add(pictureBox); }

                    else if(lstChckBox[i].Checked == false)
                    {
                        Control ctrl;
                        for (int a = SchemalaggareInfoPanel.Controls.Count - 1; a >= 0; a--)
                        {
                            ctrl = SchemalaggareInfoPanel.Controls[a];
                            if (ctrl.Name == "VirituellPanel " + i)
                            {
                                SchemalaggareInfoPanel.Controls.RemoveAt(a);
                            }
                        }
                    }

    } }




    Ask if you dont understand..


    • Edited by Carlo Goretti Wednesday, August 19, 2020 8:33 PM
    • Moved by CoolDadTx Thursday, August 20, 2020 1:40 PM Winforms related
    Wednesday, August 19, 2020 3:50 PM

Answers

  • Try something like this:

    void Checkbox_Click(object sender, EventArgs e)

    {

       int k = -1;   

       for (int i = 0; i < lstChckBox.Count(); i++)

       {

          if (lstChckBox[i].Checked == true)

          {

             ++k;

             . . .

             panel.Location = new Point(0, (k * 100));

             . . .

    • Marked as answer by Carlo Goretti Thursday, August 20, 2020 6:59 PM
    Wednesday, August 19, 2020 6:24 PM
  • It kind of worked but didnt work all the way through.. the problem came when i unchecked one of the checkboxes..

    Did it solve the problem of extra-space?

    Probably you should clear the panel (SchemalaggareInfoPanel.Controls.Clear()) before the loop that builds the panel. The else branch is not needed.

    By the way, in case of another kind of panels FlowLayoutPanel (for SchemalaggareInfoPanel) — your child panels will be positioned automatically.



    • Edited by Viorel_MVP Thursday, August 20, 2020 7:53 AM
    • Marked as answer by Carlo Goretti Thursday, August 20, 2020 6:59 PM
    Thursday, August 20, 2020 7:40 AM

All replies

  • Try something like this:

    void Checkbox_Click(object sender, EventArgs e)

    {

       int k = -1;   

       for (int i = 0; i < lstChckBox.Count(); i++)

       {

          if (lstChckBox[i].Checked == true)

          {

             ++k;

             . . .

             panel.Location = new Point(0, (k * 100));

             . . .

    • Marked as answer by Carlo Goretti Thursday, August 20, 2020 6:59 PM
    Wednesday, August 19, 2020 6:24 PM
  • It kind of worked but didnt work all the way through.. the problem came when i unchecked one of the checkboxes..
    Wednesday, August 19, 2020 8:33 PM
  • Hi Carlo Goretti,

    Thank you for posting here.

    The code for adding controls is not appropriate.

    For the convenience of demonstration, when a checkbox is selected, I only add a label to the second panel.

    The code is like this:

            private void Checkbox_Click(object sender, EventArgs e)
            {
               
                #region
                int n = -1;
                for (int i = 0; i < lstChckBox.Count(); i++)
                {
                    if (lstChckBox[i].Checked == true)
                    {
                        ++n;
                        Label lblRubrik_1 = new Label();
                        lblRubrik_1.Name = "label " + i;
                        lblRubrik_1.Font = new Font(box.Font.FontFamily, 8);
                        lblRubrik_1.AutoSize = true;
                        lblRubrik_1.Text = lblRubrik_1.Name;
                        lblRubrik_1.Location = new Point(0, n * 100);
                        panel2.Controls.Add(lblRubrik_1);
    
                    }
                    else
                    {
                        Control ctrl;
                        for (int a = panel2.Controls.Count - 1; a >= 0; a--)
                        {
                            ctrl = panel2.Controls[a];
                            if (ctrl.Name == "label " + i)
                            {
                                panel2.Controls.RemoveAt(a);
                            }
                        }
                    }
                }
                #endregion
            }


    When I select 3 checkboxes in turn, it seems that there are only 3 labels in the second panel, but in fact, there are 6 labels, and their distribution looks like this:(Each column represents the execution of Checkbox_Click once)

    0   0   0

         1   1

              2

    The reason for this situation is that every time a checkbox is selected, the program will judge all the checkboxes, and then decide to add new controls or remove some controls in the second panel based on the state of these checkboxes.

    So if you uncheck test1 at this time, the program will remove all label1 correctly, but will add new label0 and label2 at the same time. At this time, it looks like this:

    0   0   0   0

                   2

              2

    If you uncheck test0, it will look like this:

                        2

                    2

                2

    I added a class-level variable List<Control> ControlsInPanel2 to solve this problem.

    The overall code is shown below.

            List<Control> ControlsInPanel2 = new List<Control>();
            private void Checkbox_Click(object sender, EventArgs e)
            {
                CheckBox checkBox = sender as CheckBox;
                if (checkBox.Checked == true)
                {
                    Label lblRubrik_1 = new Label();
                    lblRubrik_1.Name = "label " + checkBox.Name;
                    lblRubrik_1.Font = new Font(box.Font.FontFamily, 8);
                    lblRubrik_1.AutoSize = true;
                    lblRubrik_1.Text = lblRubrik_1.Name;
                    ControlsInPanel2.Add(lblRubrik_1);
                }
                else
                {
                    Label label = (Label)panel2.Controls.Find("label " + checkBox.Name, true).First();
                    ControlsInPanel2.Remove(label);
                }
                panel2.Controls.Clear();
    
                for (int i = 0; i < ControlsInPanel2.Count; i++)
                {
                    ControlsInPanel2[i].Location = new Point(0, i * 40);
                    panel2.Controls.Add(ControlsInPanel2[i]);
                }
            }

    You can refer to the idea of this code to modify your code.

    Hope this could be helpful.

    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.


    Thursday, August 20, 2020 7:32 AM
  • It kind of worked but didnt work all the way through.. the problem came when i unchecked one of the checkboxes..

    Did it solve the problem of extra-space?

    Probably you should clear the panel (SchemalaggareInfoPanel.Controls.Clear()) before the loop that builds the panel. The else branch is not needed.

    By the way, in case of another kind of panels FlowLayoutPanel (for SchemalaggareInfoPanel) — your child panels will be positioned automatically.



    • Edited by Viorel_MVP Thursday, August 20, 2020 7:53 AM
    • Marked as answer by Carlo Goretti Thursday, August 20, 2020 6:59 PM
    Thursday, August 20, 2020 7:40 AM