Reputation: 133
I'm creating a WPF application in c# and i'm wondering how to delete dynamically created button in grid assuming that i know buttons name.
Explanation: I have 16 buttons and i want to delete two buttons at the same time when i clicked one button. I Have their names but i don't know how to use this.
this is what i tried but this is useful only when you're trying delete clicked button.
public void delete(Button x)
{
myGrid.Children.Remove(x);
}
I hope you understand my question because my English is not my native language.
My code:
for (int i=1; i<17; i++)
{
Button x = new Button();
x.MinHeight = 100;
x.MinWidth = 100;
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"C:path\slikeSpomin\hrbtnaStran.png", UriKind.Relative));
x.Background = brush;
int ime = r.Next(0, max);
//x.Content = pari[ime];
x.Name = pari[ime];
pari.RemoveAt(ime);
max--;
x.VerticalAlignment = VerticalAlignment.Top;
x.HorizontalAlignment = HorizontalAlignment.Left;
x.Margin = new Thickness(a, b, 0, 0);
a = a + 130;
if (i % 4 == 0)
{
b = b + 120;
a = 10;
}
x.Click += new RoutedEventHandler((obj, ev) => btnClick(x));
gumbiDodani.Insert(0, x);
gridSlike.Children.Add(x);
} // Here i create my buttons
private void btnClick(Button x)
{
var brush = new ImageBrush();
string slika = x.Name;
sliki.Add(slika);
brush.ImageSource = new BitmapImage(new Uri(@"path\slikeSpomin\" + slika + ".png", UriKind.Relative));
x.Background = brush;
//zaženem metodo, ki preverja enake in se izvede samo v primeru, če so enake
enake(slika, x);
}
List<string> ksad = new List<string>();
public void enake(string ime, Button x)
{
ksad.Add(ime);
if (ksad.Count == 2)
{
string prva = ksad[0];
string druga = ksad[1];
if (prva.Equals(druga + "a") || druga.Equals(prva + "a"))
{
string imeGumba = x.Name;
MessageBox.Show(prva + " " + druga);
//this.gridSlike.Children.re
gridSlike.Children.Remove(x);
ksad.Clear();
}
else
{
MessageBox.Show("happy");
ksad.Clear();
}
}
}
Upvotes: 0
Views: 2385
Reputation: 99
Maybe it is because of not disposing of it. (Many people are missing that point.) Let's take a code from AsfK's answer:
sp.Children.Remove(btn);
Try to change it into that:
btn.Dispose();
(İf the code below doesn't work or gives an error try that: "sp.Children.btn.Dispose();
")
Hope it helps.
Upvotes: 0
Reputation: 3476
Use this piece of code:
UIElement btn = null;
foreach (Control c in sp.Children)
if (c.Name == "Button1")
btn = (UIElement)c;
sp.Children.Remove(btn);
Btw, I found the reason why this code sp.Children.Remove((UIElement)this.FindName("Button0"));
not works here:
Any additions to the element tree after initial loading and processing must call the appropriate implementation of RegisterName for the class that defines the XAML namescope. Otherwise, the added object cannot be referenced by name through methods such as FindName. Merely setting a Name property (or x:Name Attribute) does not register that name into any XAML namescope.
Upvotes: 1
Reputation: 1230
i think you can do something like this lets say the button name is B1
List<Button> remove = new List<Button>();
foreach (var children in myGrid.Children)
{
if ((children.GetType() == typeof(Button)))
{
if ((children as Button).Name == "B1")
remove.Add(children as Button);
}
}
foreach (var ch in remove)
{
myGrid.Children.Remove(ch as Button);
}
or simply
myGrid.Children.Remove((UIElement) this.FindName("B1"));
like Asfk mentioned in his answer
Upvotes: 1