为了让学生对事件有一个初步的印象,特意设计了一个趣味程序,界面较简单,就是提一个简单的问题,要你点Yes或No按钮来回答问题。但当你去点Yes按钮的时候,这个Yes按钮会满窗口的跑,不让你去点它。 
这个趣味程序是在教学过程中临时穿插进去的。使用这个程序的目的,是让学生对C#中对事件的处理有一个初步的印象,了解事件要经过注册和实现这两步,另外通过这个趣味程序,让学生了解Random类和Point类的使用。 本程序的全部代码如下,请注意红字的代码,其余的代码都是设计器自动生成的。 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace LoveMe { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Button yesButton; private System.Windows.Forms.Button noButton; /// <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.yesButton = new System.Windows.Forms.Button(); this.noButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Font = new System.Drawing.Font("Monotype Corsiva", 36F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label1.ForeColor = System.Drawing.Color.ForestGreen; this.label1.Location = new System.Drawing.Point(40, 72); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(320, 80); this.label1.TabIndex = 0; this.label1.Text = "Do you love me?"; // // yesButton // this.yesButton.Location = new System.Drawing.Point(56, 248); this.yesButton.Name = "yesButton"; this.yesButton.TabIndex = 1; this.yesButton.Text = "Yes"; this.yesButton.Click += new System.EventHandler(this.yesButton_Click); this.yesButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.yesButton_MouseMove); // // noButton // this.noButton.Location = new System.Drawing.Point(240, 248); this.noButton.Name = "noButton"; this.noButton.TabIndex = 1; this.noButton.Text = "No"; this.noButton.Click += new System.EventHandler(this.noButton_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(400, 317); this.Controls.Add(this.yesButton); this.Controls.Add(this.label1); this.Controls.Add(this.noButton); this.Name = "Form1"; this.Text = "想说爱我不容易"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void noButton_Click(object sender, System.EventArgs e) { MessageBox.Show("Oh,Bye-bye!","I'm sorry",MessageBoxButtons.OK,MessageBoxIcon.Warning); this.Close(); } private void yesButton_Click(object sender, System.EventArgs e) { MessageBox.Show("Thank you, I'm very happy!","Happy",MessageBoxButtons.OK,MessageBoxIcon.Information); } private void yesButton_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Random rand = new Random(unchecked((int)DateTime.Now.Ticks)); int x=rand.Next(0,this.Size.Width-100); int y=rand.Next(0,this.Size.Height-50); Point point=new Point(x,y); yesButton.Location=point; } } } 
|