📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Exam2_13
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(381, 22);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button5);
this.groupBox1.Controls.Add(this.button4);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(14, 36);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(380, 43);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "档位模拟";
//
// button5
//
this.button5.Location = new System.Drawing.Point(290, 17);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(50, 20);
this.button5.TabIndex = 4;
this.button5.Tag = "4";
this.button5.Text = "倒档";
this.button5.Click += new System.EventHandler(this.button1_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(226, 17);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(50, 20);
this.button4.TabIndex = 3;
this.button4.Tag = "3";
this.button4.Text = "3档";
this.button4.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(162, 17);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(50, 20);
this.button3.TabIndex = 2;
this.button3.Tag = "2";
this.button3.Text = "2档";
this.button3.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(98, 17);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(50, 20);
this.button2.TabIndex = 1;
this.button2.Tag = "1";
this.button2.Text = "1档";
this.button2.Click += new System.EventHandler(this.button1_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(34, 17);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(50, 20);
this.button1.TabIndex = 0;
this.button1.Tag = "0";
this.button1.Text = "启动";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(407, 85);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "事件示例";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Break b=new Break();//建立档类实例
b.ChangeBreak+=new BreakHandler(b_ChangeBreak);//事件挂接
BreakArgs ee=new BreakArgs();//新建BreakArgs实例,并传入档为数值
ee.Level=Convert.ToInt32(((Button)sender).Tag);
b.OnChangeBreak(sender,ee);//引发事件
}
private void b_ChangeBreak(object sender,BreakArgs e)
{
switch(e.Level)
{
case 0:
label1.Text="您启动了汽车,时速10";
break;
case 1:
label1.Text="当前档位:1档,时速20";
break;
case 2:
label1.Text="当前档位:2档,时速40";
break;
case 3:
label1.Text="当前档位:3档,时速80";
break;
case 4:
label1.Text="当前档位:倒档,时速-5";
break;
}
}
}
public delegate void BreakHandler(object sender,BreakArgs e);
//建立能够传递档位数值的参数类型
public class BreakArgs:EventArgs
{
private int i;
public int Level
{
get{ return i;}
set{ i=value;}
}
}
//建立一个类,该类声明了一个事件
public class Break
{
public event BreakHandler ChangeBreak;
public void OnChangeBreak(object sender,BreakArgs e)
{
if(ChangeBreak!=null)
{
ChangeBreak(sender,e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -