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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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.
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...
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:
In the Timer's Tick event, put this code:
Timers aren't really accurate to the millisecond, so this won't take exactly 4 seconds, but it will do the job.