21

I have dynamically generated controls on the panels of windows form and i have also generated a button for removing the controls, all in rows.

int c = 0;
private void button1_Click(object sender, EventArgs e)
{
    int v;
    v = c++;
    panel1.VerticalScroll.Value = VerticalScroll.Minimum;
    ComboBox combo = new ComboBox();
    combo.Name = "combobox" + v ;
    combo.Location = new Point(30, 5 + (30 * v));

    ComboBox combo2 = new ComboBox();
    combo2.Name = "combobox2" + v ;
    combo2.Location = new Point(170, 5 + (30 * v));

    TextBox txt = new TextBox();
    txt.Name = "txtbx" + v;
    txt.Location = new Point(300, 5 + (30 * v));

    TextBox txt2 = new TextBox();
    txt2.Name = "txtbx2" + v;
    txt2.Location = new Point(450, 5 + (30 * v));

    TextBox txt3 = new TextBox();
    txt3.Name = "txtbx3" + v;
    txt3.Location = new Point(600, 5 + (30 * v));

    Button btn = new Button();
    btn.Name = "btn" + v;
    btn.Text = "Remove";
    btn.Location = new Point(750, 5 + (30 * v));
    panel1.Controls.Add(combo);
    panel1.Controls.Add(btn);
    panel1.Controls.Add(txt);
    panel1.Controls.Add(combo2);
    panel1.Controls.Add(txt2);
    panel1.Controls.Add(txt3);
    btn.Click += new EventHandler(btn_Click);
    combo.Tag = btn; 
    combo2.Tag = combo; 
    btn.Tag = combo2;

}

 private void btn_Click(object sender, EventArgs e)
 {
     ComboBox cb3 = btnh.Tag as ComboBox;
     ComboBox cb4 = cb3.Tag as ComboBox;
     panel1.Controls.Remove(cb3);
     panel1.Controls.Remove(cb4);
     panel1.Controls.Remove(btnh);
  }

Now how do I remove all the controls from a row upon clicking a button from that row?

nawfal
  • 70,104
  • 56
  • 326
  • 368
shariq_khan
  • 671
  • 4
  • 16
  • 33
  • What controls do you want to remove? All the controls? Or a select few? In that case which all controls? What have you tried? – nawfal Dec 15 '12 at 01:53
  • i was trying diffrent method, but i heard about this. so i decided to work with tablelayout, i was using tag property to bind the object and panel.controls.remove method to remove the control but it doesnt work when i have many controls, Can you help me – shariq_khan Dec 15 '12 at 02:00
  • 2
    It does not work because your button click handler is empty. You need to post the code that you have written that "doesn't work". And describe _how_ it does not work. From your other postings I would suspect that you 1. are not setting the Tag property just as you are not setting it here in this posting and 2. trying to cast the Tag to be a control. – PhoenixReborn Dec 15 '12 at 02:43
  • okay, i will handle, update my question – shariq_khan Dec 15 '12 at 09:30

6 Answers6

51

You are still not saying which control you want to remove, what type of controls you want to remove or how you want to identify them.

You could just loop through the controls to remove specific Controls.

If you have Linq, its easy:

private void btn_Click(object sender, EventArgs e)
{
    panel1.Controls.Clear(); //to remove all controls


    //to remove all comboboxes
    foreach (Control item in panel1.Controls.OfType<ComboBox>().ToList())
    {
        panel1.Controls.Remove(item); 
    }


   //to remove control by Name
    foreach (Control item in panel1.Controls.OfType<Control>().ToList())
    {
        if (item.Name == "bloodyControl")
            panel1.Controls.Remove(item); 
    }

    
    //to remove just one control, no Linq
    foreach (Control item in panel1.Controls)
    {
        if (item.Name == "bloodyControl")
        {
             panel1.Controls.Remove(item);
             break; //important step
        }
    }
}

Edit:

Its easy to do the same since you're tagging the control already. All you need is to just retrieve the control back from tag. But you need to tag appropriately:

Do this instead:

private void button1_Click(object sender, EventArgs e)
{
    int v;
    v = c++;
    panel1.VerticalScroll.Value = VerticalScroll.Minimum;

    Button btn = new Button();
    btn.Name = "btn" + v;
    btn.Text = "Remove";
    btn.Location = new Point(750, 5 + (30 * v));
    btn.Click += new EventHandler(btn_Click);
    
    ComboBox combo = new ComboBox();
    combo.Name = "combobox" + v ;
    combo.Location = new Point(30, 5 + (30 * v));
    combo.Tag = btn;

    ComboBox combo2 = new ComboBox();
    combo2.Name = "combobox2" + v ;
    combo2.Location = new Point(170, 5 + (30 * v));
    combo2.Tag = btn;

    TextBox txt = new TextBox();
    txt.Name = "txtbx" + v;
    txt.Location = new Point(300, 5 + (30 * v));
    txt.Tag = btn;

    TextBox txt2 = new TextBox();
    txt2.Name = "txtbx2" + v;
    txt2.Location = new Point(450, 5 + (30 * v));
    txt2.Tag = btn;

    TextBox txt3 = new TextBox();
    txt3.Name = "txtbx3" + v;
    txt3.Location = new Point(600, 5 + (30 * v));
    txt3.Tag = btn;

    panel1.Controls.Add(combo);
    panel1.Controls.Add(btn);
    panel1.Controls.Add(txt);
    panel1.Controls.Add(combo2);
    panel1.Controls.Add(txt2);
    panel1.Controls.Add(txt3);    
}

private void btn_Click(object sender, EventArgs e)
{
   //to remove control by Name
    foreach (Control item in panel1.Controls.OfType<Control>().ToList())
    {
        if (item.Tag == sender || item == sender)
            panel1.Controls.Remove(item); 
    }
}

Here you are tagging controls with the button, hence on the button click you can remove all the controls whose tags are the clicked button which you get from sender argument. But the downside of this approach is that you have to enumerate all the controls of the panel which is not great.

Edit: As I came to learn the below code is for a table layout panel which the OP isn't using for now. But anyway a table panel layout is better suited for this job.

I would suggest you to do this:

private void button1_Click(object sender, EventArgs e)
{
    int v;
    v = c++;
    panel1.VerticalScroll.Value = VerticalScroll.Minimum;

    Button btn = new Button();
    btn.Name = "btn" + v;
    btn.Text = "Remove";
    btn.Location = new Point(750, 5 + (30 * v));
    btn.Click += new EventHandler(btn_Click);
    btn.Tag = v;

    ComboBox combo = new ComboBox();
    combo.Name = "combobox" + v ;
    combo.Location = new Point(30, 5 + (30 * v));
    combo.Tag = v;

    ComboBox combo2 = new ComboBox();
    combo2.Name = "combobox2" + v ;
    combo2.Location = new Point(170, 5 + (30 * v));
    combo2.Tag = v;

    TextBox txt = new TextBox();
    txt.Name = "txtbx" + v;
    txt.Location = new Point(300, 5 + (30 * v));
    txt.Tag = v;

    TextBox txt2 = new TextBox();
    txt2.Name = "txtbx2" + v;
    txt2.Location = new Point(450, 5 + (30 * v));
    txt2.Tag = v;

    TextBox txt3 = new TextBox();
    txt3.Name = "txtbx3" + v;
    txt3.Location = new Point(600, 5 + (30 * v));
    txt3.Tag = v;

    panel1.Controls.Add(combo);
    panel1.Controls.Add(btn);
    panel1.Controls.Add(txt);
    panel1.Controls.Add(combo2);
    panel1.Controls.Add(txt2);
    panel1.Controls.Add(txt3);    
}

private void btn_Click(object sender, EventArgs e)
{
    int toBeDeletedRow = (int)((Control)sender).Tag;
    for (int row = panel1.RowCount - 1; row >= 0; row--)
    {
        if (row == toBeDeletedRow)
        {
            panel1.RowStyles.RemoveAt(row);
            panel1.RowCount--;
            return;
        }
    }
}
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • sir i have to delete the control which are in line, as you can see the code, my control is generated by iterating through c++, which is y axis so the control is in line, i want to remove the control which are in line of button, iwant to remove the row of control, if i will remove the specific control which are textbox and comboboxes then it will remove all the controls from the panel, but i want to remove the control which are in row with the button which is a delete button – shariq_khan Dec 15 '12 at 09:42
  • 'System.Windows.Forms.Panel' does not contain a definition for 'RowCount' and no extension method 'RowCount' accepting a first argument of type 'System.Windows.Forms.Panel' could be found, this error is giving and also an error in rowstyles – shariq_khan Dec 15 '12 at 11:47
  • first one is working sir,thank you, can you explain the tag meaning,i dont get in forums, can you explain in local language, thank you really sir for helping. – shariq_khan Dec 15 '12 at 11:54
  • 1
    @shariq_khan I'll tell you, but it can be hard for you to follow as you are so new to all this. But I tell you this is very very very easy once u understand whats happening. When you are dynamically adding controls to a form, you need to have some reference of them, but you wouldnt have. So you are tagging every dynamically created control as soon as they are created to a known object. In this case your button is one that you will have access to when you are about to delete a row. So better `Tag` every control object to corresponding `Button` of that particular row. – nawfal Dec 15 '12 at 12:45
  • 1
    Later when you are hitting the button, the event handler will have a `sender` argument which gives info on which control is calling the handler. So in that case, `sender` is your clear button itself. Now we will loop through all controls and see if their `Tag` property is our `sender` button. Only those controls of button's row will have their `Tag` as `sender` button. So you can remove, including the button itself. – nawfal Dec 15 '12 at 12:47
  • @shariq_khan The second code block is for tablelayoutpanel since you mentioned your panel is a tablelayoutpanel, but I assume its not the case. I wrote the entire answer having tablelayoutpanel in mind, but as you see some things work for all container controls like panel, groupbox etc.. The second answer didnt.. – nawfal Dec 15 '12 at 12:49
  • @shariq_khan please word your question properly as it was hard to follow your requirement initially. Good luck next time :) – nawfal Dec 15 '12 at 12:49
  • thankyou sir for explaining, next time i will make sure that i have provided all the available info, thank you once again sir, – shariq_khan Dec 15 '12 at 15:37
  • sir nawful help me here please http://stackoverflow.com/questions/14302330/changing-the-current-cell-selection-of-datagridview-to-row-select – shariq_khan Jan 13 '13 at 10:16
  • 1
    For your first and second solutions, isn't it impractical to modify the underlying collection while it's being enumerate? – user3260977 Sep 25 '14 at 16:42
  • @user3260977 you are right.. These are untested code. Sure I will add it. – nawfal Jul 23 '21 at 06:44
5

If you have a Panel o other container with more dinamicaly components for remove all or more than one you can proceed in this way.

In first step you load a component in array or list.

List<PersonalUserControl> lst = new List<PersonalUserControl>();

foreach (Object obj in this.PanelImplementationTeam.Controls)
{     
    //DO NOT REMOVE COMPONENT IN THIS LOOP BECAUSE
    //YOU DECREASE THE .Controls  ARRAY AND YOU WILL HAVE AN ERROR      
    if (obj is PersonalUserControl)
    {
        lst.Add((PersonalUserControl)obj);
    }
}

foreach(PersonalUserControl uc in lst)
{
    uc.Dispose();
}
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
daniele3004
  • 13,072
  • 12
  • 67
  • 75
2

If all of your controls that you want to delete are in a Panel, you can do: panel.Controls.Clear(); That clears all controls form your panel.

Flare Flax
  • 61
  • 10
0

Remove control events. From MSDN:

To remove controls from a collection programmatically

  1. Remove the event handler from the event. In Visual Basic, use the RemoveHandler Statement keyword; in Visual C#, use the -= Operator (C# Reference).
  2. Use the Remove method to delete the desired control from the panel's Controls collection.
  3. Call the Dispose method to release all the resources used by the control.

Specific examples are given using VB and C# in the linked page.

Community
  • 1
  • 1
body90
  • 21
  • This equates to throwing someone to the wolves. You don't have to remove eventhandlers and call dispose to remove an object, either, though that might be a best practice. But MSDN has some lousy examples, hence the OP's question. Plus, he's not just trying to remove one control, but a row of controls during a click event. So your #2 is the only one that applies, and you've conveniently left out code for that one, to assist. – vapcguy Apr 28 '15 at 23:03
0

Since the controls are added at the run time, register them before using the findName method using the register method.

StackPanel sp = new StackPanel
{
   Name = "mySP",
   Orientation = Orientation.Horizontal,
};

//need to register the control to find it by name
RegisterName(sp.Name, sp);

//now to find control by name 
StackPanel sp = (StackPanel)mainStackPanel.FindName("mySP");

//deleting the control found
mainStackPanel.Children.Remove(sp);

//if you need to use the same name again, you have to unregister too
UnregisterName(sp.Name);
Upulie Han
  • 431
  • 1
  • 7
  • 15
0

you can remove it by Find method plus Remove method.

1.Firstly you have to make something unique for your generated group of TextBoxes or buttons (like same name or something), after you can hit "delete button" with this code. Run it in loop to delete all in one step.

Controls.Remove(Controls.Find("unique_Name", true).LastOrDefault());

I tried to make many buttons and after delete it from the last one. Hope this helps.

2.Do not forget remove it also from your collection, this can be done by finding and removing the lastOne.

yourCollection.RemoveAt(yourCollection.Count - 1);

  • It is also important to unsubscribe event handlers and dispose the control being removed. Otherwise, this will cause a leak that may or may not eventually be handled by a 2nd generation garbage collection. – dodexahedron Jul 20 '23 at 03:10
  • That is true, I added it to solution :) – Jirka Kaja Jul 31 '23 at 07:58