中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
C#教学经验谈(2):会跑的按钮
作者:未知 时间:2005-07-27 21:42 出处:CSDN 责编:chinaitpower
              摘要:C#教学经验谈(2):会跑的按钮

  为了让学生对事件有一个初步的印象,特意设计了一个趣味程序,界面较简单,就是提一个简单的问题,要你点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;
  }

 }
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有