Remove specified Children element of a Grid

I need to remove at runtime a specified element of a Grid (grid1). This is the code where i add the elements.

 examControls.Add(excontrol);  // add the element on the ArrayList
 excontrol.Margin = new Thickness(x, y + margin, 0, 0);
 grid1.Children.Add(excontrol);   

How can i remove at runtime a specified "excontrol" element (added at runtime) ?

Thanks in advance

Upvotes: 6

Views: 26389

Answers (2)

tnw
tnw

Reputation: 13877

grid1.Children.Remove(excontrol) //edited per your edit -- this is exactly what ChrisF posted though

or

grid1.Children.RemoveAt(index)

Upvotes: 6

ChrisF
ChrisF

Reputation: 137128

If you keep a record of the control you can simply do:

grid1.Children.Remove(excontrol);

If you don't have a variable that holds the control you wish to remove you'll have to identify it in some way (Tag, Name), then find that control in the grid's children and then call Remove.

Upvotes: 13

Related Questions