主题:再来求助下,项目马上要交
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace dazi
{
public partial class Form1 : Form
{
Label[] arry;
int flag=0;
int num=2;
Random ran = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void method()
{
if (arry != null)
{
for (int i = 0; i < arry.Length; i++)
{
arry[i].Dispose();
}
}
arry = new Label[num];
for (int i = 0; i < arry.Length; i++)
{
arry[i] = new Label();
arry[i].Visible = true;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Font = new Font("宋体", 16);
arry[i].Left = ran.Next(20, this.pictureBox1.Width);
this.pictureBox1.Controls.Add(arry[i]);
}
}
private void btnbegin_Click(object sender, EventArgs e)
{
try
{
Thread t = new Thread(new ThreadStart(method));
t.Start();
Thread.Sleep(200);
//this.method();
}catch(Exception eqq)
{
MessageBox.Show(eqq.ToString());
}
for (int i = 0; i < arry.Length; i++)
{
arry[i].Visible = true;
}
this.timer1.Enabled = true;
this.Form1_Load(sender, e);
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < arry.Length; i++)
{
arry[i].Top += 20;
if (arry[i].Top > this.pictureBox1.Height)
{
arry[i].Top = -10;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(2, pictureBox1.Width - 2);
//this.lblfalse.Text = (Convert.ToInt32(lblfalse.Text) + 1).ToString();
}
}
flag++;//累加时间
//double t = Double.Parse(lbltrue.Text);
//double f = Double.Parse(lblfalse.Text);
}
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
for (int i = 0; i < arry.Length; i++)
{
// e.Handled = true;
if (e.KeyChar == Convert.ToChar(arry[i].Text))
{
arry[i].Top = -10;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(5,pictureBox1.Width-2);
//this.lbltrue.Text = (Convert.ToInt32(lbltrue.Text) + 1).ToString();
}
}
}
}
}
--------------
namespace dazi
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.btnbegin = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(-1, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(782, 373);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// btnbegin
//
this.btnbegin.Location = new System.Drawing.Point(32, 395);
this.btnbegin.Name = "btnbegin";
this.btnbegin.Size = new System.Drawing.Size(75, 23);
this.btnbegin.TabIndex = 1;
this.btnbegin.Text = "begin";
this.btnbegin.UseVisualStyleBackColor = true;
this.btnbegin.Click += new System.EventHandler(this.btnbegin_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(779, 430);
this.Controls.Add(this.btnbegin);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.KeyPreview = true;
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button btnbegin;
private System.Windows.Forms.Timer timer1;
}
}
怎么我用线程来做会有错误呢!
在线等
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace dazi
{
public partial class Form1 : Form
{
Label[] arry;
int flag=0;
int num=2;
Random ran = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void method()
{
if (arry != null)
{
for (int i = 0; i < arry.Length; i++)
{
arry[i].Dispose();
}
}
arry = new Label[num];
for (int i = 0; i < arry.Length; i++)
{
arry[i] = new Label();
arry[i].Visible = true;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Font = new Font("宋体", 16);
arry[i].Left = ran.Next(20, this.pictureBox1.Width);
this.pictureBox1.Controls.Add(arry[i]);
}
}
private void btnbegin_Click(object sender, EventArgs e)
{
try
{
Thread t = new Thread(new ThreadStart(method));
t.Start();
Thread.Sleep(200);
//this.method();
}catch(Exception eqq)
{
MessageBox.Show(eqq.ToString());
}
for (int i = 0; i < arry.Length; i++)
{
arry[i].Visible = true;
}
this.timer1.Enabled = true;
this.Form1_Load(sender, e);
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < arry.Length; i++)
{
arry[i].Top += 20;
if (arry[i].Top > this.pictureBox1.Height)
{
arry[i].Top = -10;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(2, pictureBox1.Width - 2);
//this.lblfalse.Text = (Convert.ToInt32(lblfalse.Text) + 1).ToString();
}
}
flag++;//累加时间
//double t = Double.Parse(lbltrue.Text);
//double f = Double.Parse(lblfalse.Text);
}
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
for (int i = 0; i < arry.Length; i++)
{
// e.Handled = true;
if (e.KeyChar == Convert.ToChar(arry[i].Text))
{
arry[i].Top = -10;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(5,pictureBox1.Width-2);
//this.lbltrue.Text = (Convert.ToInt32(lbltrue.Text) + 1).ToString();
}
}
}
}
}
--------------
namespace dazi
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.btnbegin = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(-1, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(782, 373);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// btnbegin
//
this.btnbegin.Location = new System.Drawing.Point(32, 395);
this.btnbegin.Name = "btnbegin";
this.btnbegin.Size = new System.Drawing.Size(75, 23);
this.btnbegin.TabIndex = 1;
this.btnbegin.Text = "begin";
this.btnbegin.UseVisualStyleBackColor = true;
this.btnbegin.Click += new System.EventHandler(this.btnbegin_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(779, 430);
this.Controls.Add(this.btnbegin);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.KeyPreview = true;
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button btnbegin;
private System.Windows.Forms.Timer timer1;
}
}
怎么我用线程来做会有错误呢!
在线等