如何同步进度条与ListBox项目删除c# [英] How to Sync Progressbar with ListBox Items Removal c#
问题描述
我正在制作一个项目,其中我有一个listBox1一个timer1一个button1和一个progressBar1。
当我点击button1时,timer1启动。
I m making a project in which i got a listBox1 a timer1 a button1 and a progressBar1.
When i click button1 the timer1 starts.
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 500;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
当timer1滴答时,它会从listBox1中删除一个项目,progressBar1必须显示删除的进度。
When timer1 ticks it removes one item from listBox1 and progressBar1 must show the progress of Removal.
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
}
if (listBox1.Items.Count > 0)
{
timer1.Enabled = true;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
}
但我认为上面的代码在progressBar1中遇到了一些错误,因为它没有显示进度而删除项目,当listBox1 items = 0时它已满。
But i think the above code got some bug with progressBar1 as it dont show progress while removal of items and it is full when the listBox1 items = 0.
推荐答案
你好朋友,当listBox1 items = 0时,进度条显示为full代码:
Hello friend, the progress bar is showing full when the listBox1 items = 0 because of the following code:
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
这里设置最大
属性为0,下一行设置为值
为0,表示进度已100%完成,因此显示已满。
现在尝试以下代码更改:
Here you're setting Maximum
property as 0 and the next line you're setting Value
as 0 which means progress is 100% complete hence showing full.
Now try the following code changes:
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = false;
}
}
- DD
- DD
试试这个
Try This
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
progressBar1.Value = 0;
groupBox1.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = false;
}
}
这篇关于如何同步进度条与ListBox项目删除c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!