Click here to Skip to main content
15,898,792 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I knw how to add dynamic buttons to window after a click event on predefined button say "show dynamic btns"

C#
for(int i=0;i<4;i++)                     
{Button bz= new Button();
                         bz.Height = 50;
                         bz.Width = 50;
                         Grid gg=new Grid(); // dynamic grid that add bz
                         gg.Children.Add(bz);
                         ThumGrid.Children.Add(gg);//predefined Grid
}

this will create 4 dynamic grid and buttons. now i want to remove all of them eg. bz and gg..pls tell me how can i do that?..
Posted

Note that the children of the grid, is just another collection. The Add and Remove methods work just like any collection. Interesting thing is that the Remove needs a UIElement, so if you go through the collection with a foreach, you get an object, which needs to be converted to a UIElement in order to remove. The Clear method is obviously your best choise, but you can do a selective remove using the Remove method
 
Share this answer
 
Comments
codeBegin 6-Jun-12 7:06am    
my 5
Maciej Los 6-Jun-12 18:18pm    
Good answer, my 5!
Nearly everything that has Add, also has Remove. What to make sure? Please see:

http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.aspx[^].

Now, what's wrong with your ability to read the documentation? Perhaps you need one more link:
Microsoft Q209354.

—SA
 
Share this answer
 
v3
Comments
codeBegin 6-Jun-12 7:06am    
my 5
Sergey Alexandrovich Kryukov 6-Jun-12 11:30am    
Thank you,
--SA
Maciej Los 6-Jun-12 18:18pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 6-Jun-12 18:25pm    
Thank you, Maciej.
--SA
You need to keep references to the Grids until you want to delete them. (The Grids keep the references to the Buttons.)

For your example, create a private field in the window class:
C#
private List<Grid> DynamicGrids = new List<Grid>();

Then add:
C#
DynamicGrids.Add(gg);
to the for loop that creates the Grids and Buttons.
Then when you want to remove them:
C#
foreach (Grid g in DynamicGrids)
{
  ThumGrid.Children.Remove(g);
}

This assumes that ThumGrid contains other elements that you don't want to remove, otherwise it is simply:
C#
ThumGrid.Children.Clear();

Be careful not to Remove elements from ThumGrid.Children while iterating over that collection with an IEnumerator (e.g., foreach) or else you will throw an exception: System.InvalidOperationException.
 
Share this answer
 
Comments
codeBegin 6-Jun-12 7:06am    
my 5
Matt T Heffron 6-Jun-12 13:03pm    
Thank you.
(I wonder why the two votes of 1?)
Maciej Los 6-Jun-12 18:17pm    
(I wonder why the two votes of 1?) - Sometimes it happens ;(
My 5!
Matt T Heffron 6-Jun-12 19:09pm    
Thanks!
mane0806 6-Jun-12 22:34pm    
i had left some Imp points..but now i got it..thankx..

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