As time passes and progressbar grows change the label text

Asked

Viewed 424 times

1

Before you start it’s just a game not a virus.

My goal is when the progressbar reaches 1% put the text as downloading viruses and when it reaches 50% put the text as Installing virus but instead it just changes to Installing virus.

This is the code so far:

Timer t = new Timer();
private void LetTheGameStart_Load(object sender, EventArgs e)
{
    timer1.Enabled = true;
    timer1.Start();
    timer1.Interval = 1000;
    progressBar1.Maximum = 10;
    timer1.Tick += new EventHandler(timer1_Tick);
}

void timer1_Tick(object sender, EventArgs e)
{
    {
        if (progressBar1.Value != 100)
        {
            progressBar1.Value++;
            label2.Text = "Downloading Virus";
        }
    }
    if (progressBar1.Value != 50)
    {
        label2.Text = "installing Virus";
    }
}
  • Winforms, WPF or WEB? What is the type of application? Please provide more information on the question.

  • c# in visual studio

  • Yes, but what kind of application? Using Winforms or WPF?

  • I think it’s winforms. What’s the difference

  • I already know is winform

  • is already 100 but still with the same problem and the progressibar is not increasing :(

  • @Pekira Try as follows: http://pastebin.com/ncZ780NN Maybe the error is in if (progressBar1.Value != 50) should be: if (progressBar1.Value >= 50). See if it works.

  • When the progression is <=50 showcase Downloading Virus and when it is <= 100 showcase Installing Virus, that’s it?

  • When it gets to 10 stop and I’ve already changed to 100 the max

  • not at first or 1% Installing Virus appears and when it reaches 50% shows Downloading Virus

  • I did it here, but only in a different way than yours

  • how did?

  • I will publish the reply

Show 8 more comments

1 answer

4


When using the Progressbar it is very useful to use the Backgroundworker to increase the bar (property Value from Progressbar) and leave the application free to do other things.

Implementing the Backgroundworker

First let’s implement the event DoWork of BackgroundWorker to report on progress:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{            
    for (int i = 1; i <= 100; i++) //loop para o progressbar
    {
        Thread.Sleep(100);
        backgroundWorker1.ReportProgress(i);
    }
}

Then you must implement the event ProgressChanged of BackgroundWorker to get the values being passed to Progressbar and manipulated:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    Text = e.ProgressPercentage.ToString();

    if (progressBar1.Value >= 1 && progressBar1.Value <= 50)
    {
        label1.Text = "Installing Virus";                
    }
    else if (progressBar1.Value > 50 && progressBar1.Value <= 100)
    {
        label1.Text = "Downloading Virus";
    }
}

And finally just call the method RunWorkerAsync() Backgroundworker to activate it:

private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

PS: The property WorkerReportsProgress Backgroundworker has to be set to true to update the progress.

Exit:

When the value is below 50 display the message Installing Virus:

print 1

When the value is above 50 display the message Downloading Virus:

print 2

You can also use the event RunWorkerCompleted Backgroundworker to do something when progress is completed.

Source: http://www.dotnetperls.com/progressbar

  • you can send me the code it’s not giving me the code not of the error but nothing happens

  • @Pekira here is the code.

  • I don’t know what’s going on if I send you the program files you can help me?

  • If you have more questions, ask other questions, ask on the points where you have doubts, this way is better to help.

  • When I click the button nothing happens

  • Try to run the complete project, here are the files.

  • thanks for the help :D

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.