views:

479

answers:

3

I have a label on a splash screen that is displayed for 4 seconds. I am trying to make the label display the loading process as a percentage. Obviously, this is just to show the user that the program is actually starting up and not actually "loading" anything. Is there a way that I can have the label display the percentage (going from 1% to 100%) within 4 seconds? A bit lost on how to do this.

+3  A: 

Put a Timer control on the form, and set its Interval property to 40 and its Enabled property to true. Create a form-level variable like this:

private int _Progress = 0;

In the Timer's Tick event, put this code:

if (_Progress < 100)
{
    _Progress++;
    label1.Text = _Progress.ToString() + "%";
}
else
{
    timer1.Enabled = false;
}

Timers aren't really accurate to the millisecond, so this won't take exactly 4 seconds, but it will do the job.

MusiGenesis
The problem is that your solution requires the program to be running, and he wants something that shows the program is loading while it is actually in the process of starting.
James Black
@James: seriously? Do a splash screen in unmanaged C++? Howitzers tend to make a big mess when you use them on mice. :)
MusiGenesis
@Nate: that probably means it's really only taking 1.6 seconds to load your main form.
MusiGenesis
@James, I thought Nate wanted a 'cosmetic' progress text and not a real loading behind the scene.
o.k.w
@Nate: generally speaking, it's impossible to show loading progress like this if you don't know exactly how long it takes to load your main form, and if you have no way of breaking up the total load time into discrete elements.
MusiGenesis
I say 'lock' the splashscreen until the timer tick completed 4 seconds (or there about).
o.k.w
How do I programmatically lock the form?
Nate Shoffner
@Nate: I think `o.k.w.` just meant to not allow the splash screen to be closed until its `_Progress` variable has reached 100. You could expose `_Progress` with a public property, and then have whatever code closes the splash form just keep checking this property in a loop (with a `DoEvents()` or a `Thread.Sleep(x)`) until it reaches 100, and *then* close the splash form.
MusiGenesis
I just tried that out and it works great! Thank you very much for all your time and help. Truly appreciated.
Nate Shoffner
@MusiGenesis: Yea that's what I meant by 'lock'. Sorry for the poor choice of word. Glad it worked for Nate :)
o.k.w
+1  A: 

Assuming you're talking WinForms (not WPF), the simplest way would be a timer control. Set the timeout for 40 ms (4 secs = 4000 ms. 4000 ms/100 updates = 40 ms). Create a class-level integer for tracking progress. Then your code for the OnTick event would look something like this...

if(progress < 100)
{
  progress++;
  progessLabel.Text = String.Format("Loading...  Progress: {0}%", progress);
}
else
{
  timer.Enabled = false;
}
Cam Soper
It's *deja vu* all over again!
MusiGenesis
A: 

A timer with the interval set to say 100 milliseconds would be the simplest approach. Keep a count of the number of times the timer event is called and update the progress bar by 2.5 percent each tick.

While this would work, I'd say that a progress bar is not ideal for this situation. Instead just an animated graphic would be better as it gives an indication that your program is starting up, but does not mislead like a progress bar can.

I think Microsoft regularly make this mistake of using misleading progress bars in certain applications.

Ash
Netscape used to do this with a progress bar that would go all the way to the right and then turn around and start moving back to the left, and so on for as long as the loading took (which was usually hours back in the 90s).
MusiGenesis