mar*_*rco 0 c# visual-studio-2010 winforms
我有一个间隔为1分钟的计时器,我想与它并行增加一个进度条.我正在使用Winforms和C#.我怎样才能做到这一点 ?
请帮帮我
Alo*_*n M 14
以下是如何将Timer控件与进度条一起使用的示例.首先,创建一个新的Timer和一个ProgressBar.然后,使用此函数开始加载表单的时间:
timer1.Enabled = true; // Enable the timer.
timer1.Start();//Strart it
timer1.Interval = 1000; // The time per tick.
Run Code Online (Sandbox Code Playgroud)
然后,为tick创建一个事件,如下所示:
timer1.Tick += new EventHandler(timer1_Tick);
Run Code Online (Sandbox Code Playgroud)
创建事件的功能:
void timer1_Tick(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
在此之后,将代码添加到tick函数,为进度条增加值,类似于:
progressBar1.Value++;
Run Code Online (Sandbox Code Playgroud)
不要忘记为进度条设置最大值,您可以通过将此代码添加到该form_load函数来执行此操作:
progressBar1.Maximum = 10; // 10 is an arbitrary maximum value for the progress bar.
Run Code Online (Sandbox Code Playgroud)
另外,不要忘记检查最大值,以便计时器停止.您可以使用以下代码停止计时器:
timer1.Stop();
Run Code Online (Sandbox Code Playgroud)
完整代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)