2008年(787)
分类:
2008-09-25 16:10:57
为了对事件有一个初步的印象,特意设计了一个趣味程序,界面较简单,就是提一个简单的问题,要你点Yes或No按钮来回答问题。但当你去点No按钮的时候,这个No按钮会满窗口的跑,不让你去点它。 使用这个程序的目的,是让我们对C#中对事件的处理有一个初步的印象,了解事件要经过注册和实现这两步,另外通过这个趣味程序,让我们了解Random类和Point类的使用。
本程序的全部代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace LoveMe
{
///
/// Form1 的摘要说明。
///
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;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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);
//
// 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);
this.noButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.noButton_MouseMove);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 317);
this.Controls.Add(this.noButton);
this.Controls.Add(this.yesButton);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "想说不爱我不容易";
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[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 noButton_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);
noButton.Location=point;
}
}
}