C# - 使用code添加按钮单击事件 [英] C# - Add button click events using code

查看:149
本文介绍了C# - 使用code添加按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎0 C#和总体规划的经验,所以你可能会发现我的问题愚蠢的。但是,我试着去创建一个Windows窗体使用code和我有我想要的成功。但现在我想按钮单击事件添加到我的所有按钮。我想addToDay [I]以清除exerciseBox [I],setBox [i]和repBox [I]文字。谢谢你。

 公共NewSchedule2(字符串路径)
    {
        的InitializeComponent();
        this.SuspendLayout();        标签=新System.Windows.Forms.Label [7];
        演习=新System.Windows.Forms.TextBox [7];
        套=新System.Windows.Forms.TextBox [7];
        代表=​​新System.Windows.Forms.TextBox [7];
        addToDay =新System.Windows.Forms.Button [7];        字符串[] =行File.ReadAllLines(路径);        的for(int i = 0;我7;;我++)
        {
            this.labels [I] =新System.Windows.Forms.Label();
            this.labels [I] .Location =新System.Drawing.Point(40,40 + I * 50);
            this.labels [I] .Name点=标签+我;
            this.labels [I] .Size =新System.Drawing.Size(110,20);
            this.labels [I]。文本=行[I]
            this.Controls.Add(this.labels [I]);            如果(行[I] ==Restday)
            {            }
            其他
            {
                this.exercises [I] =新System.Windows.Forms.TextBox();
                this.exercises [I] .Location =新System.Drawing.Point(160,40 +我* 50);
                this.exercises [I] .Name点=excersiceBox+我;
                this.exercises [I] .Size =新System.Drawing.Size(110,20);
                this.exercises [I]。文本=;
                this.Controls.Add(this.exercises [I]);                this.sets [I] =新System.Windows.Forms.TextBox();
                this.sets [I] .Location =新System.Drawing.Point(290,40 +我* 50);
                this.sets [I] .Name点=setBox+我;
                this.sets [I] .Size =新System.Drawing.Size(40,20);
                this.sets [I]。文本=;
                this.Controls.Add(this.sets [I]);                this.reps [I] =新System.Windows.Forms.TextBox();
                this.reps [I] .Location =新System.Drawing.Point(350,40 +我* 50);
                this.reps [I] .Name点=repBox+我;
                this.reps [I] .Size =新System.Drawing.Size(40,20);
                this.reps [I]。文本=;
                this.Controls.Add(this.reps [I]);                this.addToDay [I] =新System.Windows.Forms.Button();
                this.addToDay [I] .Location =新System.Drawing.Point(430,40 +我* 50);
                this.addToDay [I] .Name点=addToDay+我;
                this.addToDay [I]。点击+ =新System.EventHandler(this.button_Clicked);
                this.addToDay [I] .Size =新System.Drawing.Size(80,20);
                this.addToDay [I]。文本=添加到+线[I]
                this.Controls.Add(this.addToDay [I]);
            }
        }
    }    button_Clicked私人无效(对象发件人,EventArgs的发送)
    {    }
}


解决方案

这是如何添加点击事件按钮:

  //为btnTest是按钮的对象。这是你如何为按钮添加事件
btnTest.Click + =新System.EventHandler(this.btnButton_Click);//它的事件处理程序无效btnButton_Click(对象发件人,EventArgs的发送)
{
 //你的code到这里
}

我希望它会帮助你..:)

I have almost 0 experience with C# and programming overall, so you might find my question stupid. However, Im trying to create a Windows Form using code and I've succeeded with what I've wanted. But now I would like to add button click events to all my buttons. I want addToDay[i] to clear text in exerciseBox[i], setBox[i] and repBox[i]. Thanks.

    public NewSchedule2(string path)
    {
        InitializeComponent();
        this.SuspendLayout();

        labels = new System.Windows.Forms.Label[7];
        exercises = new System.Windows.Forms.TextBox[7];
        sets = new System.Windows.Forms.TextBox[7];
        reps = new System.Windows.Forms.TextBox[7];
        addToDay = new System.Windows.Forms.Button[7];

        string[] lines = File.ReadAllLines(path);

        for (int i = 0; i < 7; i++)
        {
            this.labels[i] = new System.Windows.Forms.Label();
            this.labels[i].Location = new System.Drawing.Point(40, 40 + i * 50);
            this.labels[i].Name = "Label" + i;
            this.labels[i].Size = new System.Drawing.Size(110, 20);
            this.labels[i].Text = lines[i];
            this.Controls.Add(this.labels[i]);

            if (lines[i] == "Restday")
            {

            }
            else
            {
                this.exercises[i] = new System.Windows.Forms.TextBox();
                this.exercises[i].Location = new System.Drawing.Point(160, 40 + i * 50);
                this.exercises[i].Name = "excersiceBox" + i;
                this.exercises[i].Size = new System.Drawing.Size(110, 20);
                this.exercises[i].Text = "";
                this.Controls.Add(this.exercises[i]);

                this.sets[i] = new System.Windows.Forms.TextBox();
                this.sets[i].Location = new System.Drawing.Point(290, 40 + i * 50);
                this.sets[i].Name = "setBox" + i;
                this.sets[i].Size = new System.Drawing.Size(40, 20);
                this.sets[i].Text = "";
                this.Controls.Add(this.sets[i]);

                this.reps[i] = new System.Windows.Forms.TextBox();
                this.reps[i].Location = new System.Drawing.Point(350, 40 + i * 50);
                this.reps[i].Name = "repBox" + i;
                this.reps[i].Size = new System.Drawing.Size(40, 20);
                this.reps[i].Text = "";
                this.Controls.Add(this.reps[i]);

                this.addToDay[i] = new System.Windows.Forms.Button();
                this.addToDay[i].Location = new System.Drawing.Point(430, 40 + i * 50);
                this.addToDay[i].Name = "addToDay" + i;
                this.addToDay[i].Click += new System.EventHandler(this.button_Clicked);
                this.addToDay[i].Size = new System.Drawing.Size(80, 20);
                this.addToDay[i].Text = "Add To " + lines[i];
                this.Controls.Add(this.addToDay[i]);
            }
        }
    }

    private void button_Clicked(object sender, EventArgs e)
    {

    }
}

解决方案

This is how you can add click events for buttons:

// btnTest is object of button. This is how you add event for button
btnTest.Click += new System.EventHandler(this.btnButton_Click);

// its event handler

void btnButton_Click(object sender, EventArgs e)
{
 // your code goes here
}

I hope it will help you.. :)

这篇关于C# - 使用code添加按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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