Click here to Skip to main content
15,887,946 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i have windows form which have a button, when button is clicked it dynamically generated controls, also a button which is dynamically generated is added so that it will remove the controls which are in line, means aside the button, a row of control will be removed when button clicked my code is
int c = 0;
        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)// this is the dynamically added button's event which will remove the combobox and textbox
        {
            Button btnh = sender as Button;
            foreach (Control item in panel1.Controls.OfType<TextBox>())
            {
                if (item.Tag == sender || item == sender)
                    panel1.Controls.Remove(item);
            }
            foreach (Control item in panel1.Controls.OfType<ComboBox>())
            {
                if (item.Tag == sender || item == sender)
                    panel1.Controls.Remove(item);
            }
            panel1.Controls.Remove(btnh);
        }

my error is nothing but the problem is it dosnt removes all the controls it leaves controls and i dont know whats the problem my code is simple and easy but i dont know where it lacks
Posted
Comments
Jibesh 15-Dec-12 15:23pm    
Have you tried panel1.Controls.Clear() to clear all the controls that added to the panel.

are you sure its the controls inside the panel1 and not removing the panel itself.
sariqkhan 16-Dec-12 0:27am    
sir i dont want to clear all the controls i want to remove the control in a line, a row of control
Jibesh 16-Dec-12 0:34am    
something you are missing here. what do you mean by control in a line. share the image of your expectation so that all can understand what you really need.

also there is no line concept in .Net class "Panel" it just add the controls to its list and you are arranging the position to display it like line by line.

there are already have solution how to iterate though the child control in a panel, you can check the name of the each control what you want delete. Remember you can store any object under the Tag property of the control, so define a new class keep your control specific data under that and use that inside the button delete click.
Sergey Alexandrovich Kryukov 15-Dec-12 22:53pm    
Did you try to execute it under debugger? It would show you everything. Your approach looks highly overcomplicated. If you want to find a control to remove by tag, make it a tag of the button, not the other way around you designed, by some weird reason. Did you get the idea?
—SA
sariqkhan 16-Dec-12 0:29am    
here is get the best way to remove the control in a row, without using tabellayout is this one, which is tagging, controls are not much more, so i used this,

Here is the another best thing you can do.

instead of Panel use, TableLayoutPanel since you said the arrangement controls is like line by line.

say
Line1 - you have set of TextBox,ComboBox and Buttons
Line2 - contains another set of TextBox,ComboBox and Buttons

Place a TableLayoutPanel with 1 row and 1 column
Place a Panel1 Control at Row(0),Column(0) position and set its Dock status - FILL

Place another Panel2 Control at Row(1) Column(0) position and set its dock status - FILL

use Panel1.Clear to remove all the controls in Line 1 in your language.

keep the controls in Panel2 as such.Untouched!!!
 
Share this answer
 
Comments
sariqkhan 16-Dec-12 1:49am    
thank you sir,
shaikh-adil 16-Dec-12 1:54am    
+5
Jibesh 16-Dec-12 2:35am    
Thanks man
shaikh-adil 16-Dec-12 8:22am    
your welcome, i should say thanks as you solved my problem
Jibesh 16-Dec-12 8:25am    
you both same school :>.. kidding ;)
It is not a best way, apparently, even if you did not make a mistake in it. You are looking for a control to be removed, right? Make a reference to it a tag after creation. A tag of what? Of something which is already known at the moment of the click: the button. For example:
C#
ComboBox aComboBoxToBeRemovedLater = new ComboBox();

//...

Button aRemovingButton = new Button();
aRemovingButton.Text = "&Remove";
aRemovingButton.Tag = aComboBoxToBeRemovedLater;

aRemovingButton.Click += (sender, eventArgs) => {
    Button thisButton = (Button)sender;
    Control controlToRemove = (Control)thisButton.Tag;
    thisButton.Parent.Controls.Remove(controlToRemove);
    //... maybe even remove the button itself, adjust layout, etc...
} //aRemovingButton.Click

//...

// add the button, the combo box and other controls to the parent here


—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 16-Dec-12 19:11pm    
Nice addition :-D
Sergey Alexandrovich Kryukov 16-Dec-12 19:26pm    
Thank you, Espen.
—SA
Jibesh 16-Dec-12 19:39pm    
did you read OP is comment they way he arrange the control on the panel appears line by line means some controls he doesnt want to delete from the panel when remove button is clicked.

might be setting the Tag of each control to null for the controls to be persistent and Tag as removeButton for the rest of the control.

This solution may not meet this requirement. since you did the reverse, setting the only one control to the Button. There exists only one remove button and many Text and ComboBox controls
Sergey Alexandrovich Kryukov 16-Dec-12 22:07pm    
Of course, but then the code is easily generalized by putting the object of the type Control[] in the tag. My code is just the simplest example.
—SA
sariqkhan 16-Dec-12 23:48pm    
thank you sir, i got your idea

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900