-1

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?

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
  • Please add more information to the question by editing. What are you having problems with? Is something not working? Please add a question for others to answer. – Sami Kuhmonen Oct 29 '17 at 13:35
  • You right.I edit the question – Caner Ünver Oct 29 '17 at 13:47
  • The code you have posted isn't complete and so it is hard to understand, especially with the foreign language variable names. It may be best if you post a more complete example of the code you currently have, and what you have tried so far to get it do what you want. – Callum Watkins Oct 29 '17 at 14:21
  • private void btnSil_Click(object sender, EventArgs e) { this.Controls.Remove(gBKisaMetin); gBKisaMetin.Dispose(); } – Caner Ünver Oct 29 '17 at 14:24
  • @CanerÜnver preferably in your question body. – Callum Watkins Oct 29 '17 at 14:25
  • `gBKisaMetin` will not be available in the click handler because it is a local variable in your `Form1_Load`. Does each GroupBox have an associated delete button? – Callum Watkins Oct 29 '17 at 14:28
  • @CallumWatkins yes each groupbox have association an delete buttons – Caner Ünver Oct 29 '17 at 14:31

2 Answers2

0

So since the GroupBox is the parent of the delete Button, you should be able to access it very simply like this:

private void btnSil_Click(object sender, EventArgs e)
{
    Control groupBox = ((Button)sender).Parent;
    this.Controls.Remove(groupBox);
    groupBox.Dispose();
}

This casts the sender to a Button, then accesses the parent control (the GroupBox) via the Parent property. The GroupBox can then be manipulated.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
0

You could also store a reference to the GroupBox in the Tag property of your Button when you create it:

Button btnSil = new Button();
btnSil.Tag = gBKisaMetin;

Now you can retrieve that reference in the click handler:

private void btnSil_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GroupBox gb = (GroupBox)btn.Tag;
    gb.Dispose(); // will automatically remove it as well
}

This approach differs in that the Button does not need to be inside the GroupBox for this to work.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40