Newbie with Progress Bar Question

W

What To Do

Hi All,

Sorry newbie here ...... I have a Splash Screen and am trying to get a
progress bar to work on the bottom. My splash screen stays on for 8000ms and
I would like the bar to go up in time with that.

I have the following code:

private void frmSplash_Load(object sender, EventArgs e)
{
prbSplash.Minimum = 0;
prbSplash.Maximum = 8000;
{
}
int value;
for (value = 0; value != 8000; value++)
{
prbSplash.Value = prbSplash.Value + 1;
}
}

This seems to just wizz up though. How can I slow it down a bit?

Cheers Everyone
Si
 
J

Jeff Johnson

Sorry newbie here ...... I have a Splash Screen and am trying to get a
progress bar to work on the bottom. My splash screen stays on for 8000ms
and I would like the bar to go up in time with that.

I have the following code:

private void frmSplash_Load(object sender, EventArgs e)
{
prbSplash.Minimum = 0;
prbSplash.Maximum = 8000;
{
}
int value;
for (value = 0; value != 8000; value++)
{
prbSplash.Value = prbSplash.Value + 1;
}
}

This seems to just wizz up though. How can I slow it down a bit?

If your splash screen is staying up for 8 seconds you must have some means
of determining when that 8 seconds is up. (I'll go out on a limb and say a
timer...?) Use that same method to update the progress bar.
 
W

What To Do

Yep I have a timer in place:)

Sorry im a totally newbie to C# and Development in general. Could you
possible supply me with a link or example code to help me?

Im guessing i need to change my code below to use the timer?

Cheers
Si
 
J

Jeff Johnson

Yep I have a timer in place:)

Sorry im a totally newbie to C# and Development in general. Could you
possible supply me with a link or example code to help me?

Im guessing i need to change my code below to use the timer?

No, you need to MOVE your code into the timer event. Hopefully you do NOT
simply have your timer interval set to 8000ms. If so, change it to something
smaller (let's say 80ms, 1% of the total time your splash screen displays)
and then have a private variable (a "field," i.e., a variable at the form
level, NOT a variable local to the timer event handler) and increment that
variable by 1 every time the event fires. Then you can set the value of your
progress bar to this amount (change the max of your progress bar to 100
instead of the 8000 it appears to be at). That way the event should be
called 100 times and you'll run your progress bar up to 100%. (Throw in some
error checking just to be sure your variable doesn't go above 100.)

I'm explaining this in words instead of giving you code because there's
nothing better to help a (self-proclaimed) newbie than actually writing
code.
 
P

Peter Duniho

What said:
Yep I have a timer in place:)

Sorry im a totally newbie to C# and Development in general. Could you
possible supply me with a link or example code to help me?

Im guessing i need to change my code below to use the timer?

My first advice is for you to not set a fixed time for your splash
screen to be displayed. Forcing a user to wait 8 seconds just because
you think he should is likely to annoy the user.

If you actually have some lengthy initialization that has to occur, then
that initialization should be able to report its progress, and that is
the progress that should be indicated in the ProgressBar as it changes.

If you are actually waiting 8 seconds arbitrarily, then the progress is
the passage of time. You can set a timer for some smaller interval
(e.g. 100ms), and then update the progress bar each time the timer
fires. For example:

private void frmSplash_Load(object sender, EventArgs e)
{
prbSplash.Minimum = 0;
prbSplash.Maximum = 8000;

DateTime dtStart = DateTime.Now;
System.Windows.Forms.Timer timer = new
System.Windows.Forms.Timer();

timer.Interval = 100;
timer.Tick += (sender, e) =>
{
int ms = (int)(DateTime.Now -
dtStart).TotalMilliseconds;
if (ms < prbSplash.Maximum)
{
prbSplash.Value = ms;
}
else
{
prbSplash.Value = prbSplash.Maximum;
timer.Stop();

// Do whatever here to clean up
// the splash screen. For example...
this.Close();
}
};

timer.Start();
}

Or something like that.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top