我有一個項目,其中我有一個listBox1一個timer1一個button1和一個progressBar1。如何同步進度條與列表框項目刪除c#
當我點擊button1時,timer1啓動。
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 500;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
當定時器1蜱從listBox1中和progressBar1刪除一個項目必須顯示刪除進度。
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一些bug,因爲它不顯示進度,同時取走物品,它是全時爲ListBox項目= 0
進度條在您的代碼中不起作用。 –
你是否啓用了按鈕點擊定時器?在我們設置進度條上的最大值之前,還有列表框項目嗎? – Avneesh
是的,我已啓用計時器並設置click.yes列表框項目生成之前單擊事件。 –