Home > Enterprise >  How to remove dynamically added label in C# WFA?
How to remove dynamically added label in C# WFA?

Time:09-29

I have a problem when trying to remove a label from controls. I have 6 tabpages in a tabcontrol that should have the same output. I used a checkbox to add the elements, which works fine. Whenever i try to remove a label from the controls, the label is still visible. I am unsure if im refering to the label incorrectly or if there is some other way to do this. Also tried using Dispose() with no success. Please excuse the bad english.

 private void tabpages_Feltolt()
    {
        int index = 0;
        foreach (TabPage tab in tabControl1.Controls)
        {
            if (tabControl1.Controls.IndexOf(tab)!=tabControl1.Controls.IndexOf(tabPage_Meret))
            {
                CheckBox checkBox_sikmaras = new CheckBox()
                {
                    Parent = this,
                    Location = new Point(10, 10),
                    AutoSize = true,
                    Tag=index,
                };
                Label label_checkbox_sikmaras = new Label()
                {
                    Parent = this,
                    Location = new Point(25, 10),
                    AutoSize = true,
                    Text = "Síkmarás",
                };
                checkBox_sikmaras.CheckedChanged  = CheckBox_sikmaras_CheckedChanged;
                Control[] controls = new Control[]
                {
                checkBox_sikmaras,
                label_checkbox_sikmaras,
                };
                tab.Controls.AddRange(controls);
            }
            index  ;
        }
    }

    private void CheckBox_sikmaras_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox sikmaras = (CheckBox)sender;
        TabPage tab = tabControl1.TabPages[(int)sikmaras.Tag];
        Label label_sikmaras_tabla = new Label()
        {
            Parent = this,
            Location = new Point(10, 25),
            AutoSize = true,
            Text = "Síkmarás",
            Name = Convert.ToString(sikmaras.Tag),
        };
        Control[] controls = new Control[]
        {
            label_sikmaras_tabla,
        };
        if (sikmaras.Checked)
        {
            tabControl1.TabPages[(int)sikmaras.Tag].Controls.AddRange(controls);
        }
        else
        {
            foreach (var control in controls)
            {
                if (tab.Controls.Contains(control))
                {
                    Control controltoremove = tabControl1.TabPages[Convert.ToInt32(sikmaras.Tag)].Controls[Convert.ToString(sikmaras.Tag)];
                    tabControl1.TabPages[(int)sikmaras.Tag].Controls.Remove(controltoremove);
                }
            }
        }
        
    }

CodePudding user response:

First. You are creating a new instance of Label in CheckBox_sikmaras_CheckedChanged every time it's called. That's why if (tab.Controls.Contains(control)) always returns false. Although their fields are identical it's not the same label.

Second. Why get controltoremove from TabPage, if you already know that it contains it from if (tab.Controls.Contains(control))? Just remove it.

Here is example of working code.

public Form1()
    {
        InitializeComponent();

        label_sikmaras_tabla = new Label()
        {
            Location = new Point(10, 25),
            AutoSize = true,
            Text = "Síkmarás",
            //Name = Convert.ToString(sikmaras.Tag),
        };
    }

    Label label_sikmaras_tabla;

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox sikmaras = (CheckBox)sender;
        sikmaras.Tag = 1;
        TabPage tab = tabControl1.TabPages[(int)sikmaras.Tag];
        Control[] controls = new Control[]
        {
            label_sikmaras_tabla,
        };
        if (sikmaras.Checked)
        {
            tabControl1.TabPages[(int)sikmaras.Tag].Controls.AddRange(controls);
        }
        else
        {
            foreach (var control in controls)
            {
                if (tab.Controls.Contains(control))
                {
                    //Control controltoremove = tabControl1.TabPages[Convert.ToInt32(sikmaras.Tag)]
                    //    .Controls[Convert.ToString(sikmaras.Tag)];

                    tabControl1.TabPages[(int)sikmaras.Tag].Controls.Remove(control);
                }
            }
        }
    }
  • Related