如果我点击按钮它的加载如此之快,我怎么能将我的progressBar值设置为0?
我的代码是
private void button1_Click(object sender, EventArgs e)
{
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
if (progressBar1.Value == 100)
{
progressBar1.Value = 0;
}
Refresh();
}
}
答案 0 :(得分:0)
您的代码完全符合您的要求。 您可以添加睡眠以查看内容。
private void button1_Click(object sender, EventArgs e)
{
if(IWantToClearTheProgressbarBeforeWork == true)
progressBar1.Value = 0;
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
Thread.Sleep(100); // only for testing. Blocks your thread.
}
if(IWantToClearTheProgressbarImediatelyAfterWorkIsDone == true)
progressBar1.Value = 0;
}
答案 1 :(得分:0)
private async void button1_Click(object sender, EventArgs e)
{
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
}
await Task.Delay(2000);
if (progressBar1.Value == 100)
{
progressBar1.Value = 0;
}
}