它每一秒都for一次,结果还是9啊,当然直接显示9啦
应该写成每一秒都+一次而不是直接for
int i = 1; //定义变量
Timer_Tick的代码:
testbox1.Text = (i++).ToString();
if (i == 9) MessageBox.Show("已经执行到9了");
//直接用这个代替你那长长的for
在工具栏里拖个timer到应用程序,然后:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int num=1;//定义一个全局变量
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;可以在timer属性里设置,也可以在代码里设置时间
timer1.Start();
}
//双击你拖的那个timer即可生成下面的事件,里面的代码我想你应该能看懂
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = num.ToString();
num++;
if(num==10)
{
timer1.Stop();
MessageBox.Show("赶紧给分啊!");
}
}
}
}
首先将timer的Interval设置为1000, 1000=1秒,然后在timer1的Tick事件中写上:
int n=0;
private void timer1_Tick(object sender, EventArgs e)
{
testbox.Text=n.ToString();
if(n>=9)
{
n=0;
}
else
n++;
}
//在button的单机事件中写上:
timer1.Start();
加一句延时执行:
System.Threading.Thread.Sleep(1000);
单位是毫秒,给分给分。