【C#与.net】6.0 事件驱动机制
2021-02-27 本文已影响0人
bobokaka
1.0 事件
image.png选中组件,在属性栏可以查看事件列表。
image.png
可以双击图形化的按钮,或者双击属性事件列表的click,都可以自动生成点击事件监听方法。
FormMain.cs
private void InitializeComponent()
{
this.btnTest = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(364, 175);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(97, 35);
this.btnTest.TabIndex = 0;
this.btnTest.Text = "测试按钮的事件";
this.btnTest.UseVisualStyleBackColor = true;
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(847, 402);
this.Controls.Add(this.btnTest);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "事件测试";
this.ResumeLayout(false);
}
FormMain.Designer.cs
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
//事件响应方法
private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show("你好!");
}
}
2.0 事件关联控制
事件可以根据我们的需要灵活地断开或者关联,比如我们新增两个按钮,代码如下:
private void InitializeComponent()
{
this.btnTest = new System.Windows.Forms.Button();
this.btnSwitch1 = new System.Windows.Forms.Button();
this.btnLink = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(364, 175);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(97, 35);
this.btnTest.TabIndex = 0;
this.btnTest.Text = "测试按钮的事件";
this.btnTest.UseVisualStyleBackColor = true;
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
//
// btnSwitch1
//
this.btnSwitch1.Location = new System.Drawing.Point(33, 23);
this.btnSwitch1.Name = "btnSwitch1";
this.btnSwitch1.Size = new System.Drawing.Size(97, 35);
this.btnSwitch1.TabIndex = 1;
this.btnSwitch1.Text = "事件断开";
this.btnSwitch1.UseVisualStyleBackColor = true;
this.btnSwitch1.Click += new System.EventHandler(this.btnSwitch_Click);
//
// btnLink
//
this.btnLink.Location = new System.Drawing.Point(146, 23);
this.btnLink.Name = "btnLink";
this.btnLink.Size = new System.Drawing.Size(97, 35);
this.btnLink.TabIndex = 2;
this.btnLink.Text = "事件关联";
this.btnLink.UseVisualStyleBackColor = true;
this.btnLink.Click += new System.EventHandler(this.btnLink_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(847, 402);
this.Controls.Add(this.btnLink);
this.Controls.Add(this.btnSwitch1);
this.Controls.Add(this.btnTest);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "事件测试";
this.ResumeLayout(false);
}
private void btnSwitch_Click(object sender, EventArgs e)
{
this.btnTest.Click -= new System.EventHandler(this.btnTest_Click);
}
private void btnLink_Click(object sender, EventArgs e)
{
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
}
image.png
3.0 事件级联响应
看代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFDemo
{
/// <summary>
/// partial:部分的,限定类本身的组成部分,两个名字一样的partial会在编译的时候编译成一个类(FormMain.Designer.cs)
/// 编译器会自己去完成。
/// </summary>
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
//给三个按钮添加同一个事件响应方法
this.btnAndy.Click += new System.EventHandler(this.btn_Tracher_Click);
this.btnCarry.Click += new System.EventHandler(this.btn_Tracher_Click);
this.btnCoco.Click += new System.EventHandler(this.btn_Tracher_Click);
}
//事件响应方法
private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show("你好!");
}
private void btnSwitch_Click(object sender, EventArgs e)
{
this.btnTest.Click -= new System.EventHandler(this.btnTest_Click);
}
private void btnLink_Click(object sender, EventArgs e)
{
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
}
//事件响应的公共方法(sender:表示事件源对象)
private void btn_Tracher_Click(object sender, EventArgs e)
{
string context = ((Button)sender).Text;
MessageBox.Show(context+"您好!");
}
}
}
private void InitializeComponent()
{
this.btnTest = new System.Windows.Forms.Button();
this.btnSwitch1 = new System.Windows.Forms.Button();
this.btnLink = new System.Windows.Forms.Button();
this.btnTestEvent = new System.Windows.Forms.Button();
this.btnAndy = new System.Windows.Forms.Button();
this.btnCarry = new System.Windows.Forms.Button();
this.btnCoco = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(364, 175);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(97, 35);
this.btnTest.TabIndex = 0;
this.btnTest.Text = "测试按钮的事件";
this.btnTest.UseVisualStyleBackColor = true;
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
//
// btnSwitch1
//
this.btnSwitch1.Location = new System.Drawing.Point(33, 23);
this.btnSwitch1.Name = "btnSwitch1";
this.btnSwitch1.Size = new System.Drawing.Size(97, 35);
this.btnSwitch1.TabIndex = 1;
this.btnSwitch1.Text = "事件断开";
this.btnSwitch1.UseVisualStyleBackColor = true;
this.btnSwitch1.Click += new System.EventHandler(this.btnSwitch_Click);
//
// btnLink
//
this.btnLink.Location = new System.Drawing.Point(136, 23);
this.btnLink.Name = "btnLink";
this.btnLink.Size = new System.Drawing.Size(97, 35);
this.btnLink.TabIndex = 2;
this.btnLink.Text = "事件关联";
this.btnLink.UseVisualStyleBackColor = true;
this.btnLink.Click += new System.EventHandler(this.btnLink_Click);
//
// btnTestEvent
//
this.btnTestEvent.Location = new System.Drawing.Point(239, 23);
this.btnTestEvent.Name = "btnTestEvent";
this.btnTestEvent.Size = new System.Drawing.Size(133, 35);
this.btnTestEvent.TabIndex = 3;
this.btnTestEvent.Text = "测试事件的级联响应";
this.btnTestEvent.UseVisualStyleBackColor = true;
//
// btnAndy
//
this.btnAndy.Location = new System.Drawing.Point(378, 23);
this.btnAndy.Name = "btnAndy";
this.btnAndy.Size = new System.Drawing.Size(133, 35);
this.btnAndy.TabIndex = 4;
this.btnAndy.Text = "Andy老师";
this.btnAndy.UseVisualStyleBackColor = true;
//
// btnCarry
//
this.btnCarry.Location = new System.Drawing.Point(517, 23);
this.btnCarry.Name = "btnCarry";
this.btnCarry.Size = new System.Drawing.Size(133, 35);
this.btnCarry.TabIndex = 5;
this.btnCarry.Text = "Carry老师";
this.btnCarry.UseVisualStyleBackColor = true;
//
// btnCoco
//
this.btnCoco.Location = new System.Drawing.Point(656, 23);
this.btnCoco.Name = "btnCoco";
this.btnCoco.Size = new System.Drawing.Size(133, 35);
this.btnCoco.TabIndex = 6;
this.btnCoco.Text = "Coco老师";
this.btnCoco.UseVisualStyleBackColor = true;
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(847, 402);
this.Controls.Add(this.btnCoco);
this.Controls.Add(this.btnCarry);
this.Controls.Add(this.btnAndy);
this.Controls.Add(this.btnTestEvent);
this.Controls.Add(this.btnLink);
this.Controls.Add(this.btnSwitch1);
this.Controls.Add(this.btnTest);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Coco老师";
this.ResumeLayout(false);
}
image.png
4.0 其他控件的常用事件。
image.pngEND