两个textbox 一个button 点击button的时候 开始运行,但是textbox2 却只动态显示了奇数次namespace TimeTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (i <= 10)
{
textBox2.Text = ("正在运行第"+i+"次");
i = i + 1;
}
else
{
timer1.Enabled = false;
textBox1.Text = "定时器已经停止";
}
} private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Tick+=new EventHandler(timer1_Tick);
textBox1.Text = ("定时器1已经启动");
}
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (i <= 10)
{
textBox2.Text = ("正在运行第"+i+"次");
i = i + 1;
}
else
{
timer1.Enabled = false;
textBox1.Text = "定时器已经停止";
}
} private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Tick+=new EventHandler(timer1_Tick);
textBox1.Text = ("定时器1已经启动");
}
}
}
正在运行第1次
正在运行第3次
正在运行第5次
正在运行第7次
挂起1秒再看if (i <= 10)
{
textBox2.Text = ("正在运行第"+i+"次");
i = i + 1;
System.Threading.Thread.Sleep(1000);
}