在WinForms中使用计时器增加ProgressBar [英] Increase a ProgressBar with Timer in WinForms

查看:47
本文介绍了在WinForms中使用计时器增加ProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个间隔为1分钟的计时器,我想同时增加一个进度条.我正在使用Winforms和C#.我该怎么办?

I have a timer with an interval of 1 minute and I would like to increase a progress bar in parallel with it. I'm using Winforms and C#. How can I do this ?

请帮助我

推荐答案

以下是如何在进度条中使用 Timer 控件的示例.首先,创建一个新的 Timer 和一个 ProgressBar .然后,使用以下功能开始加载表单的时间:

Here is an example of how to use the Timer control with a progress bar. First, create a new Timer and a ProgressBar. then, start the time when the form is loaded, using this function:

timer1.Enabled = true; // Enable the timer.
timer1.Start();//Strart it
timer1.Interval = 1000; // The time per tick.

然后,为刻度创建一个事件,如下所示:

Then, create an event for the tick, as shown:

timer1.Tick += new EventHandler(timer1_Tick);

创建事件的功能:

void timer1_Tick(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

在此之后,将代码添加到滴答功能中,从而为进度条增加价值,类似于:

After this, add code to the tick function that adds value to the progress bar, similar to this:

progressBar1.Value++;

请不要忘记为进度条设置最大值,您可以通过将以下代码添加到 form_load 函数中来做到这一点:

Don't forget to set a maximum value for the progress bar, which you can do by adding this code to the form_load function:

progressBar1.Maximum = 10; // 10 is an arbitrary maximum value for the progress bar.

此外,别忘了检查最大值,这样您的计时器将停止.您可以使用以下代码停止计时器:

Also, don't forget to check the maximum value so your timer will stop. You can stop the timer with this code:

timer1.Stop();

完整代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Start();
        timer1.Interval = 1000;
        progressBar1.Maximum = 10;
        timer1.Tick += new EventHandler(timer1_Tick);
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        if (progressBar1.Value != 10)
        {
            progressBar1.Value++;
        }
        else
        {
            timer1.Stop();
        }
    }
}

这篇关于在WinForms中使用计时器增加ProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆