Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have dynamically generated controls on the panels and i have also generated a button for removing the controls,
controls are on a line code is,


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);
          
        }
        private void btn_Click(object sender, EventArgs e)
        {
            
// what i have to write here for removing only the textbox and combobox and  button itself to be removed only the controls which are  aside the button

        }


what i have to write in the button click event for removing only the textbox and combobox and button itself to be removed the controls which are aside the button other line controls should not be effected by that,
Posted
Comments
[no name] 14-Dec-12 10:24am    
any efforts so far ??
sariqkhan 14-Dec-12 10:32am    
i have added this code in the button click event where the control is generated
combo.Tag = btn;
combo2.Tag = combo;
btn.Tag = combo2;
and this code in the removing button click

Button btnh = sender as Button;
ComboBox cb3 = btnh.Tag as ComboBox;
ComboBox cb4 = cb3.Tag as ComboBox;

panel1.Controls.Remove(cb3);
panel1.Controls.Remove(cb4);
panel1.Controls.Remove(btnh);

but how to remove txtbox which are 3 in numbers
[no name] 14-Dec-12 10:34am    
For removing, I've asked...
sariqkhan 14-Dec-12 10:43am    
this is for removing
Button btnh = sender as Button;
ComboBox cb3 = btnh.Tag as ComboBox;
ComboBox cb4 = cb3.Tag as ComboBox;
panel1.Controls.Remove(cb3);
panel1.Controls.Remove(cb4);
panel1.Controls.Remove(btnh);
sariqkhan 14-Dec-12 13:20pm    
rohit, how can i add controls to usercontrol dnamically? and validate the textbox and combobox which are present in a user control

I think it's time to introduce you to a new idea. Why not treat your three controls as a single control?

There is a type of class you can add to your project called a UserControl[^] - it's a container that you fill with your controls, and add your code to make them work together. Then, when you want to add all of the controls, you create an instance of your user control instead, and add that - it handles adding the three controls you want to draw.

I would create one, add your textbox, combobox and button at design time, and provide an event which the parent handles to remove it when requested. That way, the remove code just needs to access your Panel.Controls list, and use the Remove method with the sender parameter to take all three out at once...

(And it's really easy to do - but it makes your code a lot, lot simpler!)
 
Share this answer
 
Comments
[no name] 14-Dec-12 10:34am    
but whats wrong with the given code ?
StackQ 14-Dec-12 10:42am    
really,good answer given by OriginalGriff,it's also advantages.
sariqkhan 14-Dec-12 10:43am    
sir how to add user control and add textbox and combobox and how to remove that user control? can you expand more please
OriginalGriff 14-Dec-12 10:49am    
Add is really easy: Right click your project in the Solution Explorer, and select "Add...User Control". Give it a sensible name, and press "Add". You can then drop your controls on it.
Removing it is just the same as removing any control - find the right instance, and call the Remove method of the Contreols list with it as a parameter. The event handler gives you the control instance as the sender parameter, so it's just a single line of code...
sariqkhan 14-Dec-12 10:56am    
can i validate the combobox and textbox in that? and can i pass the value from that to the next form?
is not clear to me for remove control in line? every location in a panel?

this code remove controls texts and combos (include button)
C#
private void button2_Click(object sender, EventArgs e)
 {
     panel1.Controls.OfType<TextBox>().ToList().ForEach(t => panel1.Controls.Remove(t));
     panel1.Controls.OfType<ComboBox>().ToList().ForEach(t => panel1.Controls.Remove(t));

 }


I prove it with a windows form including the panel and the control that you are adding is incrementing a line
 
Share this answer
 
Comments
sariqkhan 14-Dec-12 11:52am    
yess it is in line as it is placed with the location int c = 0; and incremented each time a user clicks, by your code it removes all the textbox and combobox which is created
foreach (Control c in panel1.Controls)
{
if (c is TextBox)
{
panel1.Controls.Remove(c);
}
if (c is ComboBox)
{
panel1.Controls.Remove(c);
}

}

maybe this? in your event click button??
 
Share this answer
 
Comments
sariqkhan 14-Dec-12 13:20pm    
sir, how can i add controls to usercontrol dnamically? and validate the textbox and combobox which are present in a user control
hello sariqkhan

why dont you use a TableLayoutPanel? and change your part of code

for
C#
//combo.Location = new Point(30, 5 + (30 * v)); //this
Tablelayoutpanel1.Controls.Add( combo, 0, v);//for this


where v is your variable that increase column in the tablelayout...

and remove your selected row from de TablLayoupanel..

i refer this page

http://stackoverflow.com/questions/6202144/is-there-a-way-to-remove-all-controls-from-a-row-in-tablelayoutpanel[^]
 
Share this answer
 
Comments
sariqkhan 14-Dec-12 20:36pm    
sir,it makes my code lengthy and complicated if i use usercontrol it will be easy right sir?
can you help me in this
GREG_DORIANcod 7-Feb-13 9:00am    
hey sariqkhan check this!! maybe will be useful!
http://sdrv.ms/WCoM71 agregarControles.zip
sariqkhan 15-Dec-12 4:49am    
sir, there is problem in your code, when i click two rows of control is generated then no control generated in my form , no other row generates

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