none
Create Dynamic buttons for every Checkbox that is checked. RRS feed

  • Question

  • Hey!

    Im trying to make that if you check a dynamic checkbox. then a dynamic Button appears. If you uncheck it. then it removes.
    Have got it to work on one but can get it to work on everyone..
    Look here:


    Here is my code for this:

    for (int i = 0; i < files.Count(); i++)
                    {
                        box = new CheckBox();
                        btn = new Button();
                        box.Tag = i;
                        box.Font = new Font(box.Font.FontFamily, 8);
                        box.ForeColor = Color.White;
                        box.AutoSize = true;
                        box.Text = Path.GetFileName(files[i]);
                        box.Location = new Point(0, (i * 25));
                        box.CheckedChanged += new EventHandler(Checkbox_Click);
                        lstChckBox.Add(box);
    
                        GåVidareBtn.Visible = false;
                        Height = KörBtn.Location.Y + KörBtn.Height + 60;
                        KörBtn.Visible = true;
                        panel1.Controls.Add(box);
                    }
    
    
    
            void Checkbox_Click(object sender, EventArgs e)
            {
    
                for (int i = 0; i < lstChckBox.Count(); i++)
                {
                    if (lstChckBox[i].Checked == true)
                    {
                            btn.Size = new Size(75, 20);
                            btn.Location = new Point(lstChckBox[0].Location.X + lstChckBox[0].Width + 125, lstChckBox[i].Location.Y);
                            btn.FlatStyle = FlatStyle.Flat;
                            btn.FlatAppearance.BorderSize = 0;
                            btn.BackColor = Color.FromArgb(254, 66, 2);
                            btn.ForeColor = Color.White;
                            btn.Font = new Font(btn.Font.FontFamily, 8);
                            btn.Text = "Öppna";
                            btn.Click += new EventHandler(btn_Click);
                            panel1.Controls.Add(btn);
                            break;
                        
                    }
                    else if (lstChckBox[i].Checked == false)
                    {
    
                        panel1.Controls.Remove(btn);
                    }
    
                }
            }
    
    
    
    

    Very thankful for some help out..
    Cant get it to work..

    Best regards!

    • Moved by CoolDadTx Friday, September 18, 2020 1:41 PM Winforms related
    Thursday, September 17, 2020 7:08 PM

Answers

  • I suspect that the problem is in the variable "btn". I don´t see it declared within the loop where you add the button. This leads me to think that it is a class variable, so you are overwriting the same button every time that you try to add it to the Controls collection.

    Just declare Button btn; inside the Checkbox_Click method and you should be al right for adding the button.

    Removing the button (your "else" branch) will not work either way. You will need to search the Controls collection for the button you want to remove before passing it to the Remove method.

    By the way, you are looping through the list of checkboxes every time the user clicks on any checkbox, and only creating a button for the first one that is checked. Instead, use the "sender" parameter to find out which checkbox was clicked, and then add or remove the button that corresponds to the clicked checkbox.

    • Marked as answer by Carlo Goretti Friday, September 18, 2020 5:58 PM
    Friday, September 18, 2020 7:07 AM

All replies

  • I suspect that the problem is in the variable "btn". I don´t see it declared within the loop where you add the button. This leads me to think that it is a class variable, so you are overwriting the same button every time that you try to add it to the Controls collection.

    Just declare Button btn; inside the Checkbox_Click method and you should be al right for adding the button.

    Removing the button (your "else" branch) will not work either way. You will need to search the Controls collection for the button you want to remove before passing it to the Remove method.

    By the way, you are looping through the list of checkboxes every time the user clicks on any checkbox, and only creating a button for the first one that is checked. Instead, use the "sender" parameter to find out which checkbox was clicked, and then add or remove the button that corresponds to the clicked checkbox.

    • Marked as answer by Carlo Goretti Friday, September 18, 2020 5:58 PM
    Friday, September 18, 2020 7:07 AM
  • Hi Carlo,

    Thank you for posting here.

    There is another way, when adding a checkbox, use a UserControl instead.

    Write the checkBox1_CheckedChanged method in usercontrol.

            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.Checked == true)
                {
                    Button button = new Button();
                    button.Location = new Point(125, 25);
                    button.Text = "button";
                    button.Size = new Size(100, 30);
                    button.Click += Button_Click;
                    this.Controls.Add(button);
                }
                else
                {
                    Button button = this.Controls.OfType<Button>().First();
                    this.Controls.Remove(button);
                }
            }
            private void Button_Click(object sender, EventArgs e)
            {
                MessageBox.Show("test");
            }

    If you need to pass any data, you can add some properties to the usercontrol.

    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.

    Friday, September 18, 2020 7:33 AM
  • This forum is for C#-specific questions only. Please post questions related to Winforms or WPF over in the new Microsoft Q&A forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, September 18, 2020 1:39 PM
  • Solved it like this:
            void Checkbox_Click(object sender, EventArgs e)
            {
                Control.ControlCollection ctrlCollectionPanel = panel1.Controls;
                Control ctrl;
    
                for (int i = 0; i < lstChckBox.Count(); i++)
                {
                    Button btn = new Button();
                    if (lstChckBox[i].Checked == true)
                    {
                            btn.Size = new Size(75, 20);
                            btn.Name = "Dynamisk knapp " + i;
                            btn.Location = new Point(lstChckBox[0].Location.X + lstChckBox[0].Width + 115, lstChckBox[i].Location.Y);
                            btn.FlatStyle = FlatStyle.Flat;
                            btn.FlatAppearance.BorderSize = 0;
                            btn.BackColor = Color.FromArgb(254, 66, 2);
                            btn.ForeColor = Color.White;
                            btn.Font = new Font(btn.Font.FontFamily, 8);
                            btn.Text = "Öppna";
                            btn.Click += new EventHandler(btn_Click);
                            panel1.Controls.Add(btn);
                        
                    }
                    else if (lstChckBox[i].Checked == false)
                    {
    
                        for (int a = ctrlCollectionPanel.Count - 1; a >= 0; a--)
                        {
                            ctrl = ctrlCollectionPanel[a];
                            if (ctrl is Button && ctrl.Name == "Dynamisk knapp " + i)
                            {
                                ctrlCollectionPanel.RemoveAt(a);
                            }
                        }
    
                    }
    
                }
            }

    Friday, September 18, 2020 5:59 PM