c# - 在 WinForms 中增加一个带有计时器的 ProgressBar

标签 c# winforms visual-studio-2010

<分区>

我有一个间隔为 1 分钟的计时器,我想增加一个与之并行的进度条。我正在使用 Winforms 和 C#。我该怎么做?

请帮帮我

最佳答案

这是一个如何使用带有进度条的 Timer 控件的示例。首先,创建一个新的 Timer 和一个 ProgressBar。然后,使用此函数开始加载表单的时间:

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

然后,为tick创建一个事件,如图:

timer1.Tick += new EventHandler(timer1_Tick);

创建事件的函数:

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

在此之后,将代码添加到为进度条添加值的滴答函数中,类似于此:

progressBar1.Value++;

不要忘记为进度条设置最大值,您可以通过将以下代码添加到 form_load 函数来实现:

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

此外,不要忘记检查最大值,这样您的计时器就会停止。您可以使用此代码停止计时器:

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();
        }
    }
}

关于c# - 在 WinForms 中增加一个带有计时器的 ProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7259511/

相关文章:

c# - FFplay成功移入我的Winform,如何设置无边框?

visual-studio-2010 - MapPoint控件-添加地区

c# - 多个 WPF 控件使用的 WCF 双工服务

c# - 任何库将数字拼音转换为带声调标记的拼音?

带图像的 C# 按钮在单击时未获得 'pushed'

c# - DataGridView 单元格

c++ - 升级 VS2010 项目导致 '0xC0000005: Access violation reading location 0xCDCDCDCD.'

c# - 如何将参数传递给 Visual Studio 2010 的 Crystal Report?看起来他们被忽略了

c# - Nancy 和/或 TinyIoC 不尊重 AsSingleton()

具有动态 LINQ 的 C# bool 逻辑?