⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 这是在学习C#的代理和事件时写的一个例子
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace EventTest
{
	

	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblInfo;
		private System.Windows.Forms.Button btnRaise;
		private System.Windows.Forms.Label label1;

		public delegate void ActionEventHandler( object sender, ActionCancelEventArgs ev );
		public static event ActionEventHandler Action;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		protected void OnAction( object sender, ActionCancelEventArgs ev )
		{
			if( Action != null )
				Action( sender, ev );
		}

		#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.lblInfo = new System.Windows.Forms.Label();
			this.btnRaise = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblInfo
			// 
			this.lblInfo.Location = new System.Drawing.Point(24, 72);
			this.lblInfo.Name = "lblInfo";
			this.lblInfo.Size = new System.Drawing.Size(280, 16);
			this.lblInfo.TabIndex = 2;
			// 
			// btnRaise
			// 
			this.btnRaise.Location = new System.Drawing.Point(127, 112);
			this.btnRaise.Name = "btnRaise";
			this.btnRaise.TabIndex = 3;
			this.btnRaise.Text = "Click me";
			this.btnRaise.Click += new System.EventHandler(this.btnRaise_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(312, 40);
			this.label1.TabIndex = 4;
			this.label1.Text = "When now time\'s seconds is less than 30,then show \"Wasn\'t the right time.\",otherw" +
				"ise show now time.";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(328, 157);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.btnRaise);
			this.Controls.Add(this.lblInfo);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btnRaise_Click(object sender, System.EventArgs e)
		{
			BusEntity _busEntity = new BusEntity ();
			ActionCancelEventArgs cancelEvent = new ActionCancelEventArgs ();
			OnAction( this, cancelEvent );
			if( cancelEvent.Cancel )
				lblInfo.Text = cancelEvent.message ;
			else
				lblInfo.Text = _busEntity.TimeString ;
		}
	}

	public class ActionCancelEventArgs:CancelEventArgs 
	{
		string _msg = "";
		public ActionCancelEventArgs():base(){}
		public ActionCancelEventArgs(bool cancel):base(cancel){}
		public ActionCancelEventArgs(bool cancel,string message):base(cancel)
		{
			_msg = message;
		}
		public string message
		{
			set { _msg = value; }
			get { return _msg; }
		}
	}
	
	public class BusEntity
	{
		string _time = "";
		public BusEntity()
		{
			Form1.Action += new Form1.ActionEventHandler(Form1_Action);
		}

		private void Form1_Action( object sender, ActionCancelEventArgs ev )
		{
			ev.Cancel = !DoActions();
			if(ev.Cancel )
				ev.message = "Wasn't the right time.";
		}
		private bool DoActions()
		{
			bool retVal = false;
			DateTime tm = DateTime.Now;
			if(tm.Second < 30)
			{
				_time = "The time is " + DateTime.Now.ToLongTimeString ();
				retVal = true;
			}
			else
				_time = "";
			return retVal;
		}
		public string TimeString
		{
			get { return _time; }
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -