通过mouseClick事件删除组框 [英] Deleting groupboxes by mouseClick event

查看:56
本文介绍了通过mouseClick事件删除组框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为我的坏英语道歉

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.

这是我的代码(在Form2_Load中)

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 
        }

感谢您的帮助

推荐答案

为GroupBox添加一个名称,基于i.ToString(),就像你对按钮所做的那样(这样点击处理程序将知道要删除哪个GroupBox)。 />
将按钮的Tag属性设置为GroupBox,如下所示:

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;

然后,btnSil_Click中的代码应如下所示:

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);
}





这篇关于通过mouseClick事件删除组框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆