using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace RandomWord
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;

private int _count=0;
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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
// 
// timer1
// 
this.timer1.Interval = 5000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "字符的随机产生";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
this.Deactivate += new System.EventHandler(this.Form1_Deactivate); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void timer1_Tick(object sender, System.EventArgs e)
{
Label lbl1=new Label();
lbl1.Name=""+_count.ToString(); lbl1.Width=24;
lbl1.Height=24;
lbl1.ForeColor=System.Drawing.Color.White;
lbl1.BackColor=this.BackColor;
this.Controls.Add(lbl1);
System.Random random=new Random(DateTime.Now.Millisecond*DateTime.Now.Millisecond);
lbl1.Left=random.Next(this.Width); Letter letter=new Letter(lbl1,this);
ThreadStart threadStart=new ThreadStart(letter.Run);
Thread thread=new Thread(threadStart);
thread.Start();
} private void Form1_Load(object sender, System.EventArgs e)
{
timer1.Start();
} private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
foreach(Label label in this.Controls)
{
if(label==null)
break;
if(label.Text[0]==e.KeyChar)
{
label.Dispose();
this.Controls.Remove(label);
}
}
} private void Form1_Deactivate(object sender, System.EventArgs e)
{
Application.Exit();
}
}
public class Letter
{
private Label _label;
private Form _container;
private int _speed=2;
public Letter(Label label,Form container)
{
_label=label;
_container=container; Random random=new Random(DateTime.Now.Millisecond);
_speed=random.Next(5)+1; _label.Text=Convert.ToChar(65+random.Next(57)).ToString(); switch(_speed)
{
case 1:
_label.ForeColor=System.Drawing.Color.Red;
break;
case 2:
_label.ForeColor=System.Drawing.Color.Yellow;
break;
case 3:
_label.ForeColor=System.Drawing.Color.Blue;
break;
case 4:
_label.ForeColor=System.Drawing.Color.Green;
break;
case 5:
_label.ForeColor=System.Drawing.Color.White;
break;
default:
_label.ForeColor=System.Drawing.Color.White;
break;
}
}
public void Run()
{
try
{
while(_label.Top<=this._container.Height+100);
{
if(_label==null)
{
Thread.CurrentThread.Abort();
}
_label.Top+=25;
Thread.Sleep(_speed*5);
}
if(Thread.CurrentThread.IsAlive)
{
Thread.CurrentThread.Abort();
}
}
catch(Exception ex)
{
Console.WriteLine("错误:"+ex.Message);
Console.WriteLine("错误:"+ex.StackTrace);
}
finally
{
if(!_label.Disposing)
{
_label.Dispose();
}
_container.Controls.Remove(_label);
}
}
}
}

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;namespace RandomWord
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;

    private int _count=0;
    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.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Interval = 10;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "字符的随机产生";
    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Deactivate += new System.EventHandler(this.Form1_Deactivate); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    Label lbl1=new Label();
    lbl1.Name=""+_count.ToString(); lbl1.Width=24;
    lbl1.Height=24;
    lbl1.ForeColor=System.Drawing.Color.White;
    lbl1.BackColor=this.BackColor;
    this.Controls.Add(lbl1);
    System.Random random=new Random(DateTime.Now.Millisecond*DateTime.Now.Millisecond);
    lbl1.Left=random.Next(this.Width); Letter letter=new Letter(lbl1,this);
    ThreadStart threadStart=new ThreadStart(letter.Run);
    Thread thread=new Thread(threadStart);
    thread.Start();
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    timer1.Start();
    } private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    foreach(Label label in this.Controls)
    {
    if(label==null)
    break;
    if(label.Text[0]==e.KeyChar)
    {
    // label.Dispose();
    // this.Controls.Remove(label);
    }
    }
    } private void Form1_Deactivate(object sender, System.EventArgs e)
    {
    Application.Exit();
    }
    }
    public class Letter
    {
    private Label _label;
    private Form _container;
    private int _speed=2;
    public Letter(Label label,Form container)
    {
    _label=label;
    _container=container; Random random=new Random(DateTime.Now.Millisecond);
    _speed=random.Next(5)+1; _label.Text=Convert.ToChar(65+random.Next(57)).ToString(); switch(_speed)
    {
    case 1:
    _label.ForeColor=System.Drawing.Color.Red;
    break;
    case 2:
    _label.ForeColor=System.Drawing.Color.Yellow;
    break;
    case 3:
    _label.ForeColor=System.Drawing.Color.Blue;
    break;
    case 4:
    _label.ForeColor=System.Drawing.Color.Green;
    break;
    case 5:
    _label.ForeColor=System.Drawing.Color.White;
    break;
    default:
    _label.ForeColor=System.Drawing.Color.White;
    break;
    }
    }
    public void Run()
    {
    try
    {
    while(_label.Top<=this._container.Height+100)
    {
    if(_label==null)
    {
    Thread.CurrentThread.Abort();
    }
    _label.Top+=25;
    Thread.Sleep(_speed*5);
    }
    if(Thread.CurrentThread.IsAlive)
    {
    Thread.CurrentThread.Abort();
    }
    }
    catch(Exception ex)
    {
    Console.WriteLine("错误:"+ex.Message);
    Console.WriteLine("错误:"+ex.StackTrace);
    }
    finally
    {
    // if(!_label.Disposing)
    // {
    // _label.Dispose();
    // }
    // _container.Controls.Remove(_label);
    }
    }
    }
    }
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;namespace RandomWord
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;

    private int _count=0;
    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.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Interval = 10;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "字符的随机产生";
    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Deactivate += new System.EventHandler(this.Form1_Deactivate); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    Label lbl1=new Label();
    lbl1.Name=""+_count.ToString(); lbl1.Width=24;
    lbl1.Height=24;
    lbl1.ForeColor=System.Drawing.Color.White;
    lbl1.BackColor=this.BackColor;
    this.Controls.Add(lbl1);
    System.Random random=new Random(DateTime.Now.Millisecond*DateTime.Now.Millisecond);
    lbl1.Left=random.Next(this.Width); Letter letter=new Letter(lbl1,this);
    ThreadStart threadStart=new ThreadStart(letter.Run);
    Thread thread=new Thread(threadStart);
    thread.Start();
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    timer1.Start();
    } private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    foreach(Label label in this.Controls)
    {
    if(label==null)
    break;
    if(label.Text[0]==e.KeyChar)
    {
    // label.Dispose();
    // this.Controls.Remove(label);
    }
    }
    } private void Form1_Deactivate(object sender, System.EventArgs e)
    {
    Application.Exit();
    }
    }
    public class Letter
    {
    private Label _label;
    private Form _container;
    private int _speed=2;
    public Letter(Label label,Form container)
    {
    _label=label;
    _container=container; Random random=new Random(DateTime.Now.Millisecond);
    _speed=random.Next(5)+1; _label.Text=Convert.ToChar(65+random.Next(57)).ToString(); switch(_speed)
    {
    case 1:
    _label.ForeColor=System.Drawing.Color.Red;
    break;
    case 2:
    _label.ForeColor=System.Drawing.Color.Yellow;
    break;
    case 3:
    _label.ForeColor=System.Drawing.Color.Blue;
    break;
    case 4:
    _label.ForeColor=System.Drawing.Color.Green;
    break;
    case 5:
    _label.ForeColor=System.Drawing.Color.White;
    break;
    default:
    _label.ForeColor=System.Drawing.Color.White;
    break;
    }
    }
    public void Run()
    {
    try
    {
    while(_label.Top<=this._container.Height+100)
    {
    if(_label==null)
    {
    Thread.CurrentThread.Abort();
    }
    _label.Top+=25;
    Thread.Sleep(_speed*5);
    }
    if(Thread.CurrentThread.IsAlive)
    {
    Thread.CurrentThread.Abort();
    }
    }
    catch(Exception ex)
    {
    Console.WriteLine("错误:"+ex.Message);
    Console.WriteLine("错误:"+ex.StackTrace);
    }
    finally
    {
    // if(!_label.Disposing)
    // {
    // _label.Dispose();
    // }
    // _container.Controls.Remove(_label);
    }
    }
    }
    }
      

  3.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;namespace RandomWord
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;

    private int _count=0;
    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.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Interval = 10;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "字符的随机产生";
    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Deactivate += new System.EventHandler(this.Form1_Deactivate); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    Label lbl1=new Label();
    lbl1.Name=""+_count.ToString(); lbl1.Width=24;
    lbl1.Height=24;
    lbl1.ForeColor=System.Drawing.Color.White;
    lbl1.BackColor=this.BackColor;
    this.Controls.Add(lbl1);
    System.Random random=new Random(DateTime.Now.Millisecond*DateTime.Now.Millisecond);
    lbl1.Left=random.Next(this.Width); Letter letter=new Letter(lbl1,this);
    ThreadStart threadStart=new ThreadStart(letter.Run);
    Thread thread=new Thread(threadStart);
    thread.Start();
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    timer1.Start();
    } private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    foreach(Label label in this.Controls)
    {
    if(label==null)
    break;
    if(label.Text[0]==e.KeyChar)
    {
    // label.Dispose();
    // this.Controls.Remove(label);
    }
    }
    } private void Form1_Deactivate(object sender, System.EventArgs e)
    {
    Application.Exit();
    }
    }
    public class Letter
    {
    private Label _label;
    private Form _container;
    private int _speed=2;
    public Letter(Label label,Form container)
    {
    _label=label;
    _container=container; Random random=new Random(DateTime.Now.Millisecond);
    _speed=random.Next(5)+1; _label.Text=Convert.ToChar(65+random.Next(57)).ToString(); switch(_speed)
    {
    case 1:
    _label.ForeColor=System.Drawing.Color.Red;
    break;
    case 2:
    _label.ForeColor=System.Drawing.Color.Yellow;
    break;
    case 3:
    _label.ForeColor=System.Drawing.Color.Blue;
    break;
    case 4:
    _label.ForeColor=System.Drawing.Color.Green;
    break;
    case 5:
    _label.ForeColor=System.Drawing.Color.White;
    break;
    default:
    _label.ForeColor=System.Drawing.Color.White;
    break;
    }
    }
    public void Run()
    {
    try
    {
    while(_label.Top<=this._container.Height+100)
    {
    if(_label==null)
    {
    Thread.CurrentThread.Abort();
    }
    _label.Top+=25;
    Thread.Sleep(_speed*5);
    }
    if(Thread.CurrentThread.IsAlive)
    {
    Thread.CurrentThread.Abort();
    }
    }
    catch(Exception ex)
    {
    Console.WriteLine("错误:"+ex.Message);
    Console.WriteLine("错误:"+ex.StackTrace);
    }
    finally
    {
    // if(!_label.Disposing)
    // {
    // _label.Dispose();
    // }
    // _container.Controls.Remove(_label);
    }
    }
    }
    }
      

  4.   

    不好意思啊。比较忙,最近的都已经完结了。呵呵。主要现在在Run()方法当中没有利用_label.Top+=25;?没有倒啊。