C#事件技巧

当多个控件引用同一事件时可以做如下处理

namespace EventForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //挂接事件处理器方法
            button3.Click += ButtonClick;
        }

        private void ButtonClick(object sender, EventArgs e)
        {
            //判断当前按钮执行事件
            if (sender==this.button1)
            {
                textBox1.Text = "Button1";
            }
            if (sender==this.button2)
            {
                textBox1.Text = "Button2";
            }
            if (sender==this.button3)
            {
                textBox1.Text = "Button3";
            }
        }
    }
}

 

posted on 2021-01-10 15:13  达哥来到  阅读(79)  评论(0)    收藏  举报

导航