关于C# Windows 程序设计里的button click事件的分析

首先在VS2010里建立一个windows程序,只带有一个button1。点击button1,出现一个messagebox展示Hello, world!程序。我相信这个程序大家都会的。

下面是源代码

 

Form1.Designer.cs

namespace EventStudy

{

    partial class Form1

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;



        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }



        #region Windows Form Designer generated code



        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.SuspendLayout();

            // 

            // button1

            // 

            this.button1.Location = new System.Drawing.Point(90, 159);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(100, 39);

            this.button1.TabIndex = 0;

            this.button1.Text = "button1";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click);

            // 

            // Form1

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(284, 262);

            this.Controls.Add(this.button1);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);



        }



        #endregion



        private System.Windows.Forms.Button button1;

    }

}

For1.cs

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace EventStudy

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Hello, world!");

        }

    }

}

 

主要关注这几行代码

   this.button1.Click += new System.EventHandler(this.button1_Click);







private void button1_Click(object sender, EventArgs e)

{

     MessageBox.Show("Hello, world!");

}

上面的第一行代码是给button1.click事件订阅一个button1_click方法。

 

这里的this.button1.Click

        private System.Windows.Forms.Button button1;

button1是一个windows.Form.Button类的一个实例

 

Click是button1类里面的一个事件,可以想象成是这样的:

public class Button

{

    ....

   

    public event EventHandler Click;



    ...

}

因为你从上面的code可以看出Click是EventHanlder委托的一个变量。(虽然Button类里面对于click不见得就是这样定义的,但是你可以暂且当成这样定义)

 

EventHandler这个自然就是一个定义在系统层的委托:

public delegate void EventHandler(object sender, EventArgs e);

这个委托有2个参数,从参数的名字你也可以看出来参数的意思,第一个参数是事件的发送者,第二个参数是事件参数。

第一个参数好理解了,事件的发送者。如果你点击button1, 那这里的object sender就是button1了。

第二个参数看似不好理解,我们来看下定义:

    public class EventArgs

    {

        // Summary:

        //     Represents an event with no event data.

        public static readonly EventArgs Empty;



        // Summary:

        //     Initializes a new instance of the System.EventArgs class.

        public EventArgs();

    }

它其实就是个空参数,没什么意义。

 

订阅完后,你只要点击button1,系统就会调用EventHandler(this.button1_Click)里面的button1_Click方法。

在这里就是运行

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Hello, world!");

        }

当我们运行程序进行Debug的时候,可以对于这段代码有更好的认识

+        this    {EventStudy.Form1, Text: Form1}    EventStudy.Form1

+        sender    {Text = "button1"}    object {System.Windows.Forms.Button}

+        e    {X = 69 Y = 28 Button = Left}    System.EventArgs {System.Windows.Forms.MouseEventArgs}

当点击button1的时候,你可以看到

 

sender = button1

e = {X= 69 Y = 28 Button = Left}

 

这就是说传递给button1_Click的2个参数就是

 

sender = button1

e = {X= 69 Y = 28 Button = Left}

 

其实这个不难理解。因为这个方法如果没有这2个参数,它怎么能work?

  1. 如果没有sender 它怎么知道这是哪个button1 哪个object发送的方法。比如你有2个button, button1, button2。点击的时候是不同的事件,如果没有这个参数其不是button1的事件会调到button2的,button2的会调用到button1的。
  2. 第二个参数应该是在点击的时候进行实例化的。告诉你点击的位置和方式。坐标不用说了,左键的方法更是个重要参数,不然用户邮件点击是不是也会触发这个事件呢?

 

到这里对里有了一定的了解了,原来click事件是这样的:

订阅这个事件: this.button1.Click += new EventHandler(this.button1_Click);

事件代表的方法:

private void button1_Click(object sender, EventArgs e)

{

    // DO SOMETHING;

}

 

有同学就会问了,我点击这个button1,系统是怎么知道调用这个click事件的?这个就是系统的事情了,不用我们操这份心。以后有机会再做深入的研究

你可能感兴趣的:(windows)