点击c#中动态创建按钮的事件

本文关键字:按钮 事件 创建 动态 点击 | 更新日期: 2023-09-27 18:08:12

大家好,我有一个名为GroupBoxEkle()的函数,对于每个按钮点击它都有效。

private void GroupBoxEkleme()
{
    int sayGB = 0;
    int sayacSoruText = 0;
    int sayacCevapText = 0;
    int sayacKaydetButon = 0;
    //textboxların ve butonların sayısını hesaplama
    sayacSoruText = sayacSoruText + 1;
    sayacCevapText = sayacCevapText + 1;
    sayacKaydetButon = sayacKaydetButon + 1;
    //font oluştur
    Font font = new Font("Microsoft Sans Serif", 10.0f, FontStyle.Bold);
    Font font2 = new Font("Microsoft Sans Serif", 9.0f, FontStyle.Bold);
    //groupbox oluştur
    GroupBox Group = new GroupBox();
    Group.Name = "GroupBox" + sayGB;
    Group.Width = 1240;
    Group.Height = 595;
    Group.Text = "Soru & Cevap";
    Group.Font = font;
    Group.ForeColor = Color.Maroon;
    Group.Location = new Point(200, 86);
    //label oluşturma
    Label Soru = new Label();
    Soru.Text = "SORU: ";
    Soru.Font = font2;
    Soru.ForeColor = Color.Maroon;
    Soru.Location = new Point(6, 33);
    Soru.Width = 53;
    Soru.Height = 13;
    //Soru Text
    TextBox soruText = new TextBox();
    soruText.Name = "soruText" + sayacSoruText;
    soruText.Width = 1150;
    soruText.Height = 25;
    soruText.Font = font2;
    soruText.ForeColor = Color.Black;
    soruText.Multiline = true;
    soruText.Location = new Point(70, 31);
    //label oluşturma
    Label Cevap = new Label();
    Cevap.Text = "CEVAP:";
    Cevap.Font = font2;
    Cevap.ForeColor = Color.Maroon;
    Cevap.Location = new Point(6, 317);
    Cevap.Width = 53;
    Cevap.Height = 25;
    //Cevap Text
    TextBox cevapText = new TextBox();
    cevapText.Name = "cevapText" + sayacCevapText;
    cevapText.Width = 1150;
    cevapText.Height = 490;
    cevapText.Font = font2;
    cevapText.ForeColor = Color.Black;
    cevapText.Multiline = true;
    cevapText.Location = new Point(70, 67);
    //kaydet butonu oluşturma
    Button btn = new Button();
    btn.Name = "btn" + sayacKaydetButon;
    btn.Width = 75;
    btn.Height = 25;
    btn.Text = "Kaydet";
    btn.BackColor = Color.Maroon;
    btn.Font = font2;
    btn.ForeColor = Color.White;
    btn.Location = new Point(1150, 565);
    //kontrolleri ekleme
    Group.Controls.Add(btn);
    Group.Controls.Add(Soru);
    Group.Controls.Add(soruText);
    Group.Controls.Add(Cevap);
    Group.Controls.Add(cevapText);
    this.Controls.Add(Group);
}

现在我想为每个按钮点击做like;动态创建的按钮、文本框和组框将不可见或处置,新项目将出现,增加其名称+1,如groupbox1将是groupbox2 textbox1将是textbox 2。

点击c#中动态创建按钮的事件

不知道你想做什么,但这是你提到的问题的解决方案。此外,我还删除了一些控件来简化答案。

    GroupBox Group;
    TextBox cevapText;
    static int sayGB = 0;
    private void GroupBoxEkleme()
    {
        sayGB++;
        if (Group == null)
            Group = new GroupBox();
        else
        {
            Group.Dispose();
            Group = null;
            Group = new GroupBox();
        }
        Group.Name = "GroupBox" + sayGB.ToString();
        Group.Width = 1240;
        Group.Height = 595;
        Group.Text = "Soru & Cevap";
        Group.ForeColor = Color.Maroon;
        Group.Location = new Point(200, 86);
        if (cevapText == null)
            cevapText = new TextBox();
        else
        {
            cevapText.Dispose();
            cevapText = null;
            cevapText = new TextBox();
        }
        cevapText.Name = "cevapText" + sayGB.ToString();
        cevapText.Width = 1150;
        cevapText.Height = 490;
        cevapText.ForeColor = Color.Black;
        cevapText.Multiline = true;
        cevapText.Location = new Point(70, 67);
        //kontrolleri ekleme
        Group.Controls.Add(cevapText);
        this.Controls.Add(Group);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        GroupBoxEkleme();
    }