Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 500;
            progressBar1.Maximum = listBox1.Items.Count;
            progressBar1.Value = 0;
        }


When timer1 ticks it removes one item from listBox1 and progressBar1 must show the progress of Removal.

C#
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;
            }
        }


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.
Posted

Hello friend, the progress bar is showing full when the listBox1 items = 0 because of the following code:
C#
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;

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:
C#
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
 
Share this answer
 
Try This

C#
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();
                  }
               }
 
Share this answer
 
Comments
Member 10579673 15-Jun-14 7:31am    
Not working
Member 2303544 15-Jun-14 10:43am    
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;
}
}
C#
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;
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900