hbhbice

导航

一个自定义事件的处理程序

image 

界面布置如上图,这个事例是依照《C#程序设计实用教程》(唐耀)写的,

每个铵钮的Tag属性分别设置成0,1,2,3,4,-1

代码如下。

新建两个类,一个是事件类,一个是Break类

代码如下:

//Break.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WinFormEvent
{

    class Break
    {  
        public delegate void BreakHandler(object sender , BreakArgs e);
        public event BreakHandler changeBreak;

        public void onChangeBreak(object sender, BreakArgs e)
        {
            if (changeBreak != null)
            {
                changeBreak(sender, e);
            }
        }

    }
}

 

//BreakArgs.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WinFormEvent
{
    class BreakArgs:EventArgs
    {
        private int i;

        public int Level
        {
            get
            {
                return i;
            }
            set
            {
                i = value;
            }
        }

    }
}

//form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinFormEvent
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1 .Click  += new System.EventHandler(this.btnStart_Click);
            button2.Click += new System.EventHandler(this.btnStart_Click);
            button3.Click += new System.EventHandler(this.btnStart_Click);
            button4.Click += new System.EventHandler(this.btnStart_Click);
            btnBack . Click += new System.EventHandler(this.btnStart_Click);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            Break b = new Break();
            b.changeBreak += new WinFormEvent.Break.BreakHandler(b_changeBreak);
            BreakArgs ee = new BreakArgs();
            switch (Convert.ToInt32(((Button)sender).Tag))
            {
                case 0:
                    ee.Level = 0;
                    break;
                case 1:
                    ee.Level = 1;
                    break;
                case 2:
                    ee.Level = 2;
                    break;
                case 3:
                    ee.Level = 3;
                    break;
                case 4:
                    ee.Level = 4;
                    break;
                case -1:
                    ee.Level = -1;
                    break;
            }
            b.onChangeBreak(sender, ee);
        }

        void b_changeBreak(object sender, BreakArgs e)
        {
            switch (e.Level)
            {
                case 0:
                    label1.Text = "启动";
                    break;
                case 1:
                    label1.Text = "一档";
                    break;
                case 2:
                    label1.Text = "二档";
                    break;
                case 3:
                    label1.Text = "三档";
                    break;
                case 4:
                    label1.Text = "四档";
                    break;
                case -1:
                    label1.Text = "倒档";
                    break;
            }
            //throw new Exception("The method or operation is not implemented.");
        }

    }
}

 

//Form1.Designer.cs

namespace WinFormEvent
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnStart = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.btnBack = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(45, 28);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.btnBack);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.button4);
            this.groupBox1.Controls.Add(this.button3);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Controls.Add(this.btnStart);
            this.groupBox1.Location = new System.Drawing.Point(31, 86);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(589, 94);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "档位摸拟";
            //
            // btnStart
            //
            this.btnStart.Location = new System.Drawing.Point(16, 39);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new System.Drawing.Size(75, 23);
            this.btnStart.TabIndex = 0;
            this.btnStart.Tag = "0";
            this.btnStart.Text = "启动";
            this.btnStart.UseVisualStyleBackColor = true;
            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(203, 38);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Tag = "2";
            this.button2.Text = "2";
            this.button2.UseVisualStyleBackColor = true;
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(294, 38);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Tag = "3";
            this.button3.Text = "3";
            this.button3.UseVisualStyleBackColor = true;
            //
            // button4
            //
            this.button4.Location = new System.Drawing.Point(386, 38);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 3;
            this.button4.Tag = "4";
            this.button4.Text = "4";
            this.button4.UseVisualStyleBackColor = true;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(111, 38);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 4;
            this.button1.Tag = "1";
            this.button1.Text = "1";
            this.button1.UseVisualStyleBackColor = true;
            //
            // btnBack
            //
            this.btnBack.Location = new System.Drawing.Point(481, 38);
            this.btnBack.Name = "btnBack";
            this.btnBack.Size = new System.Drawing.Size(75, 23);
            this.btnBack.TabIndex = 5;
            this.btnBack.Tag = "-1";
            this.btnBack.Text = "倒档";
            this.btnBack.UseVisualStyleBackColor = true;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(651, 195);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button btnBack;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button btnStart;
    }
}

实现效果如下:

当你按下任一个铵钮时都会产生一个事件,通知Label显示相应的信息。

posted on 2010-04-21 09:26  hbhbice  阅读(257)  评论(0编辑  收藏  举报