user1770370
user1770370

Reputation: 267

How to set value of progress bar

I have written a user control using C# Winforms. In the user control, I have three textboxes:

The progress bar denotes the no. of records added to the database and its total range is set to be equal to txtQuantity.

When one or more records are duplicate, the progress bar is stopped.

My questions are:

  1. How to set the initial value of the progress bar?
  2. How to manage the progress shown by progress bar?

How I save it to the database:

for (long i = from; i < to; i++)
{
    for (int j = 0; j < (to - from); j++)
    {
        arrCardNum[j] = from + j;
        string r = arrCardNum[j].ToString();
        try
        {
            sp.SaveCards(r, 2, card_Type_ID, SaveDate, 2);
            progressBar1.Value = j;
        }
    }
}

Upvotes: 0

Views: 52521

Answers (3)

The C Shaper
The C Shaper

Reputation: 1

To change the value use:

private void timer1_Tick(object sender, EventArgs e)
    {
        progressBar2.Value = progressBar2.Value - 15;
    }

In C#

Upvotes: 0

user3428180
user3428180

Reputation: 5

You can't use loop to do this with progressbar. There is a difference between running code in for, while, do...while loops or in timers. In loops code is immediately done and you can't see this, in timers you can. Even if you try to put in loops if counters, it will not works:

for(int i=a;i<b;++i)
{
    if (cnt < 1000000)
    {
        IncrProgressBar();
        cnt++;
    }
    else
    {
        cnt = 0;
    }
}

If you want to use progressbar to do this then you must put in timer OnTick event code that adds data to database, and in this event increment progressbar value. It's similarly with changing form component's other properties (Text, Size, ...). If you want to see change on component you must use timers.

Upvotes: 0

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

Try this:

private void StartBackgroundWork() {
    if (Application.RenderWithVisualStyles)
        progressBar.Style = ProgressBarStyle.Marquee;
    else {
        progressBar.Style = ProgressBarStyle.Continuous;
        progressBar.Maximum = 100;
        progressBar.Value = 0;
        timer.Enabled = true;
    }
    backgroundWorker.RunWorkerAsync();
}

private void timer_Tick(object sender, EventArgs e) {
    progressBar.Value += 5;
    if (progressBar.Value > 120)
        progressBar.Value = 0;
}

The Marquee style requires VisualStyles to be enabled, but it continuously scrolls on its own without needing to be updated. I use that for database operations that don't report their progress.

Here is another Progress Bar Tutorial

Upvotes: 10

Related Questions