首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Winform事件有关问题

2013-03-10 
Winform事件问题如下代码,在不修改InitializeComponent的方法下,如何把button的事件赋给button1,有几种赋

Winform事件问题
如下代码,在不修改InitializeComponent的方法下,如何把button的事件赋给button1,有几种赋值方法


  public partial class Form1 : Form
    {
        private System.Windows.Forms.Button button1;
        private System.ComponentModel.IContainer components = null;
        public Form1()
        {
            InitializeComponent();
            //button = this.button1;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(157, 89);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(397, 237);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";


            this.ResumeLayout(false);
        }

        public void button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("点击了button1","提示");
        }

        internal virtual Button button
        {
            get
            {
                return this.button1;
            }
            set
            {
                if (this.button1 != null)
                {
                    this.button1.Click -= new EventHandler(this.button_Click);
                }
                this.button1 = value;
                if (this.button1 != null)
                {
                    this.button1.Click += new EventHandler(this.button_Click);
                }
            }
        }

    }

Winform事件问题
[解决办法]
不需要,只要写
this.button1.Click += (sender, e) => { button_Click.PerformClick(); }

热点排行