要实现的功能如下:
在一个LABLE里显示一个递增的数字:程序运行后,一开始LABLE里显示的是“0”,用户在TEXTBOX里写一个数字“N”,再点一下“开始”的BUTTON,这样LABLE里的数字就每隔N秒(N就是TEXTBOX里的那个数字)增加1。即一开始0,过N秒变成1,再过N秒变成2,再过N秒变成3……,直到你点了“结束”的BUTTON为止。注意,这样的变化要在界面上能够看出来的,而且规定不能用Timer控件。很简单吧,但是我就是想不出来。请各大侠出招吧。

解决方案 »

  1.   

    弄个死循环,记录当前时间,当当前时间比记录时间大N时,TextBox的内容就加1。当然,死循环这东西不太好玩,最好把它放到线程时吧。
      

  2.   

    不能用timer,那就只能用线程..但我对线程没多大研究.
      

  3.   

    你可以用函數獲取到系統時間,再借TIMER用時間相鹹呀
      

  4.   

    用多线程,以下为我写的代码,你参考一下!
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <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.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(32, 72);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    this.label1.Text = "0";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(152, 72);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(32, 128);
    this.button1.Name = "button1";
    this.button1.TabIndex = 2;
    this.button1.Text = "开始";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(152, 128);
    this.button2.Name = "button2";
    this.button2.TabIndex = 3;
    this.button2.Text = "结束";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    private System.Threading.Thread t=null;
    private int number=0;//初始化数字
    private bool bz=true;//循环判断 private void button1_Click(object sender, System.EventArgs e) {
    t=new System.Threading.Thread(new System.Threading.ThreadStart(add));
    bz=true;
    t.Start();
    } private void add() {
    while (bz) {
    if (this.textBox1.Text.Length>0)  
    System.Threading.Thread.Sleep(Int32.Parse(this.textBox1.Text)*1000);   
    number +=1;
    this.label1.Text=number.ToString();
    }
    } private void button2_Click(object sender, System.EventArgs e) {
    t.Abort();
    bz=false;
    }
    }
    }
      

  5.   

    private bool stop = false;
    private void btnStart_Click(object sender, System.EventArgs e)
    {
    this.stop = false;
    DateTime startTime = DateTime.Now;
    int interval = Int32.Parse(this.txtInterval.Text);
    interval = interval <= 0 ? 1 : interval;
    while (!stop)
    {
    this.lblTime.Text = ((DateTime.Now - startTime).Seconds / interval).ToString();
    Application.DoEvents();
    System.Threading.Thread.Sleep(interval * 1000);
    }
    }private void btnStop_Click(object sender, System.EventArgs e)
    {
    this.stop = true;
    }没用多线程,不过用的话效果会更好
      

  6.   

    float time = int.Parse(this.textBox1.Text)/1000;
    System.DateTime kaiShi = new DateTime();
    System.DateTime jieShu = new DateTime();
    kaiShi = System.DateTime.Now;
    if(kaiShi.Second<60-time)
    {
    while(jieShu.Second <kaiShi.Second+time)
    {
    jieShu = System.DateTime.Now;
    }
    }
    else
    {
    while(jieShu.Second <kaiShi.Second && jieShu.Second >=kaiShi.Second + time -60)
    {
    jieShu = System.DateTime.Now;
    }
    }上面提到的调用系统时间加死循环的方法好象不可行。
    这是我以前的方法,就是无法在用户界面上显示出来,它总是在最后一次把最后结果显示出来,也就没有看到前面所说的由1到2的过程,直接到最后的结果了。
      

  7.   

    我又把刚才fancyf的回复去试了一下,结果还是老问题,看来单线程要完成是很难了~
    刚才那个多线程的程序我试过了,效果不错。
    这样我就结了