How to set the progress bar value

I wrote a user control using C # Winforms. In a user control, I have three text fields:

  • txtStartNumber - the input is of type: int.
  • txtEndNumber - the input is of type: int.
  • txtQuantity - iput has type: int. (value = txtEndNumber - txtStartNumber)

A progress bar indicates no. records added to the database and its total range is set to txtQuantity.

When one or more entries are duplicated, the progress bar stops.

My questions:

  • How to set the initial value of the progress bar?
  • How to manage the progress displayed by the progress bar?

How to save it in 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; } } } 
-2
source share
3 answers

Try the following:

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

Marquee's style requires VisualStyles to turn on, but it continuously scrolls by itself, without the need for updates. I use this for database operations that do not report my progress.

Here is another Promotion Tutorial

+8
source

You cannot use a loop to do this with a progressbar. There is a difference between running code for, while, do ... while, loops, or timers. In a loop, the code is immediately executed, and you cannot see it, in timers you can. Even if you try to set loops, if counters, this will not work:

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

If you want to use the progressbar for this, you must put a timer in the OnTick event code that adds data to the database and in this case increases the value of progressbar. This is similar to changing the shape parameters of other properties (Text, Size, ...). If you want to see changes on a component, you must use timers.

0
source

To change the value, use:

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

In c #

0
source

Source: https://habr.com/ru/post/1447014/


All Articles