Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 14-Dec-12 4:23am
sariqkhan1.4K
Comments
Krunal Rohit at 14-Dec-12 10:24am
   
any efforts so far ??
sariqkhan at 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
Krunal Rohit at 14-Dec-12 10:34am
   
For removing, I've asked...
sariqkhan at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!)
  Permalink  
Comments
Krunal Rohit at 14-Dec-12 10:34am
   
but whats wrong with the given code ?
01010RAJ at 14-Dec-12 10:42am
   
really,good answer given by OriginalGriff,it's also advantages.
sariqkhan at 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 at 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 at 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?
OriginalGriff at 14-Dec-12 10:58am
   
Yes. And Yes - it's a control. Your form does not need to know what you do on the inside, it just gets the info it wants via Properties as normal.
sariqkhan at 14-Dec-12 13:05pm
   
sir, how can i add controls to usercontrol dnamically? and validate the textbox and combobox which are present in a user control
OriginalGriff at 14-Dec-12 14:20pm
   
If you need to add controls to a UserControl dynamically (and you probably don't - I suspect you want to add usercontrols to the panel dynamically instead) you can, in exactly the same way that you add them to a Panel - by calling the Controls.Add method. If you are adding them from the form, I would create a public method in the UserControl (AddControl for example, which gets handed a control) and have that add it to teh control: public void AddControl(Control c) { Controls.Add(c); } As far as validation, you do that within the UserControl - and pass the result back via a Property, possibly with ValidationSucceed and ValidationFailed events. The idea is to encapsulate the functions so the Control deals with itself, and passes information that the form needs back to it - without telling the Form how it does it. That means that the control becomes more independent, in the same way that you don't need to know if your car engine is carburetted or fuel injected, just put petrol in the right hole, and oil in the other. the engine takes care of it without it affecting how you drive the car. This all makes the program more reliable, because the number of interconnections drops, and the modularity increases. Did any of that make sense? :laugh:
sariqkhan at 15-Dec-12 4:48am
   
i learn by example :laugh can you explain how to add a control,please with validation
OriginalGriff at 15-Dec-12 5:00am
   
You've said that before! That isn't a good way to learn computing - trust me on this. Computing is all about application of knowledge to new situations, not about repeating exactly what has been done before - in fact a lot of computing is constructed so that you don't repeat things! Try it: You have all the bits you need, you just need to put them together in a slightly different way. It really, really, isn't difficult! This isn't me being nasty - this is something you really need to learn! :laugh:
sariqkhan at 15-Dec-12 5:05am
   
i have done this,is this only needed for user control? int v; v = c++; ComboBox combo = new ComboBox(); combo.Name = "combobox" + v; ComboBox combo2 = new ComboBox(); combo2.Name = "combobox2" + v; UserControl us = new UserControl(); us.Name = "user" + v; us.Location = new Point(100, 5 + (30 * p)); us.Controls.Add(combo); us.Controls.Add(combo2); panel1.Controls.Add(us);
01010RAJ at 14-Dec-12 10:44am
   
user controls gives us more flexibility also.
sariqkhan at 14-Dec-12 10:49am
   
can you enlarge the topic please sir, how to use that user control and removing it by button click,
OriginalGriff at 14-Dec-12 10:56am
   
Just try it - it is really easy - it's just like a "normal" control, but it keeps your controls together. I know, I know, you "like to learn by example" - but trust me, it's is very easy to do, and you will learn better by doing that looking. :laugh:
01010RAJ at 14-Dec-12 10:55am
   
They r just like a template ,u can make them according to u,and then u can use them many times. Procedure is given by sir,now try to just follow yaar,not difficult.u can do.In the user control,add form's control what u want,then build ur solution,it will be visible in toolbox,when u will build them after creation.
sariqkhan at 14-Dec-12 13:20pm
   
raj sir, how can i validate the textbox and combobox which are present in a user control
sariqkhan at 14-Dec-12 13:12pm
   
sir, how can i add controls to usercontrol dnamically? and validate the textbox and combobox which are present in a user control
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

is not clear to me for remove control in line? every location in a panel?
 
this code remove controls texts and combos (include button)
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
  Permalink  
Comments
sariqkhan at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hello sariqkhan
 
why dont you use a TableLayoutPanel? and change your part of code
 
for
//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[^]
  Permalink  
Comments
sariqkhan at 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 at 7-Feb-13 9:00am
   
hey sariqkhan check this!! maybe will be useful! http://sdrv.ms/WCoM71 agregarControles.zip
sariqkhan at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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??
  Permalink  
Comments
sariqkhan at 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

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 442
1 sanket saxena 355
2 Abhinav S 268
3 OriginalGriff 250
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid