如何 运行 计时器上的进度条 - C#

How to run a progress bar on a timer - c#

我想 运行 通过使用计时器在表单上显示进度条。

我尝试了多种方法,但一直无法正常工作。

我希望这里有人可以帮助我。

private void SplashScreen_Load(object sender, EventArgs e)
    {
        splashScreenTimer.Enabled = true;
        splashScreenTimer.Start();
        splashScreenTimer.Interval = 1000;
        progressBar.Maximum = 100;
        splashScreenTimer.Tick += new EventHandler(timer1_Tick);

    }

    private void timer_Tick(object sender, EventArgs e)
    {
        if (progressBar.Value != 10)
        {
            progressBar.Value++;
        }
        else
        {
            splashScreenTimer.Stop();
        }
    }

你正在分配event_handler喜欢

splashScreenTimer.Tick += new EventHandler(timer1_Tick);

并且您正在更改

中的 progressBar 值
private void timer_Tick(object sender, EventArgs e)
{
    if (progressBar.Value != 10)
    {
        progressBar.Value++;
    }
    else
    {
        splashScreenTimer.Stop();
    }
}

将事件处理程序更改为

splashScreenTimer.Tick += new EventHandler(timer_Tick);

或将代码移动到另一个事件处理程序 timer1_Tick,它应该在您的表单中

对于 运行 progressBar 在 4 秒内充满你可以这样做

private void Form1_Load(object sender, EventArgs e)
{
    splashScreenTimer.Enabled = true;
    splashScreenTimer.Start();
    splashScreenTimer.Interval = 30;
    progressBar.Maximum = 100;
    splashScreenTimer.Tick += new EventHandler(timer_Tick);
}

int waitingTime = 0;

private void timer_Tick(object sender, EventArgs e)
{
    if (progressBar.Value < 100)
    {
        progressBar.Value++;
    }
    else
    {
        if (waitingTime++ > 35)
            this.Close();
    }
}