I am programmatically creating GroupBoxes which include delete buttons inside them. I want to delete the GroupBoxes when these buttons are clicked, via the btnSil_Click
event.
Here is the code I have so far in form_load
:
for (i=0;i<AnaEkranForm.n1;i++)
{
GroupBox gBKisaMetin = new GroupBox();
this.Controls.Add(gBKisaMetin);
gBKisaMetin.Location = new Point(80, gYuksek);
gYuksek += 200;
gBKisaMetin.Text = "Soru " + (soruSayac++);
gBKisaMetin.Width=500;
gBKisaMetin.Height = 150;
TextBox tBSoru = new TextBox();
tBSoru.Location = new Point(30, 30);
tBSoru.Width = 400;
gBKisaMetin.Controls.Add(tBSoru);
Label labelCevap = new Label();
labelCevap.Location = new Point(30, 70);
labelCevap.Text = "Cevap";
gBKisaMetin.Controls.Add(labelCevap);
TextBox tBKisaMetinCevap = new TextBox();
tBKisaMetinCevap.Location = new Point(30, 95);
tBKisaMetinCevap.Width = 400;
gBKisaMetin.Controls.Add(tBKisaMetinCevap);
tBKisaMetinCevap.ReadOnly = true;
Button btnSil = new Button();
btnSil.Location = new Point(460, 10);
btnSil.Width = 30;
btnSil.Text = "Sil";
btnSil.Name = "btnSil_" + i.ToString();
btnSil.Click += new EventHandler(btnSil_Click);
gBKisaMetin.Controls.Add(btnSil);
}
AnaEkranForm.n1
is the number of GroupBoxes which will be created.
And here is my button click handler for deleting:
private void btnSil_Click(object sender, EventArgs e)
{
this.Controls.Remove(gBKisaMetin);
gBKisaMetin.Dispose();
}
But this isn't working because I can't access gBkisaMetin
. How can I get this to work?