none
Deleting groupboxes by mouseClick event RRS feed

  • Question

  • For beginning i apologize for my bad english

    I'm creating groupboxes with codes whichs are include delete buttons.I want to delete a groupbox by click that delete buttons.

    Here are my codes (in Form2_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);
    }
    
     private void btnSil_Click(object sender, EventArgs e)
            {
               //The codes what i need should be in here 
            }
    

    Thanks for helping

    Sunday, October 29, 2017 2:19 PM

All replies

  • Add a name to your GroupBox, based on the i.ToString() like you did for the button (this is so the click handler will know which GroupBox to remove).
    Set the button's Tag property to the GroupBox like this:

    btnSil.Tag = gBKisaMetin;

    Then, the code in your btnSil_Click should look like this:

    private void btnSil_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GroupBox gb = (GroupBox)btn.Tag;
        this.Controls.RemoveByKey(gb.Name);
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, October 29, 2017 5:08 PM
    Moderator