你需要在單獨的線程和每次迭代之間進行睡眠或使用計時器。
例如:
private int counter;
Timer t = new Timer();
Random random = new Random();
private void button1_Click(object sender, EventArgs e)
{
t.Interval = 100;
t.Tick += new EventHandler(t_Tick);
counter = 0;
t.Enabled = true;
txt3.Text = "";
}
void t_Tick(object sender, EventArgs e)
{
counter++;
int randomNumber = random.Next(100, 150);
txt3.Text = randomNumber.ToString();
if (counter >= 50)
{
t.Enabled = false;
}
}
記住這只是一個例子出百萬的方式做到這一點。他們中的很多都不錯
另一種方式將使用線程:
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(randomize));
t.Start();
}
private void randomize()
{
Random random = new Random();
txt3.Text = "";
for (int i = 0; i < 50; i++)
{
int randomNumber = random.Next(100, 150);
Invoke(new setTxtHandler(setText), randomNumber.ToString());
Thread.Sleep(100);
}
}
private void setText(string val)
{
txt3.Text = val;
}
private delegate void setTxtHandler(string val);
該循環會很快完成,你想讓它在每個數字之間有一個延遲? –
我添加了計時器但不起作用。 – user1955534
將您的更新後的代碼添加到問題 –