2011-03-25 56 views
0

我只是希望进度条只进行一个步骤,因为计时器会打一秒钟,但无法完成。请帮忙。在winforms中使用进度条

我应该在定时器的tick事件中使用一个变量i,并将i增加1。 并写入:progressBar1.Increment(i)--i试过这个,它工作。

,但为什么它用下面的代码工作不是:

public partial class Form1 : Form 
{ 
    Timer t = new Timer(); 

    public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     t.Interval = 1000; 
     t.Enabled = true; 
     t.Tick += new EventHandler(t_Tick); 
    } 
    void t_Tick(object sender, EventArgs e) 
    { 
     progressBar1.Increment(1); 
    } 

一秒的流逝,蜱事件发生时,和进度应该加1,但在这里,它只是卡住了只在一个单一的增量,即它只前进1并停止。

+3

这是一个好主意,首先添加处理程序,然后设置enabled = true – 2011-03-25 14:29:07

+2

我试了一下,你的代码工作。你的进度条的最小值和最大值是多少? – Marijn 2011-03-25 14:52:52

+0

min = 0; max = 100 – sqlchild 2011-03-26 06:33:20

回答

2

It should work。请尝试启用定时器分配事件处理程序后:

private void Form1_Load(object sender, EventArgs e) 
{ 
    t.Interval = 1000; 
    t.Tick += new EventHandler(t_Tick); 
    t.Enabled = true; 
} 
+0

先生,你能告诉我这是怎么工作的,我的意思是,Tick事件何时被触发?何时将t.Enabled设置为true或否? – sqlchild 2011-03-26 04:30:21

+0

为什么在事件处理程序之前启用定时器时不起作用? – sqlchild 2011-03-26 06:32:13

+0

也请用例子以简单的语言告诉我关于maximun和minimum属性? – sqlchild 2011-03-26 06:33:03

-1

Missing计时器开始,下面的代码假设工作。

public partial class Form1 : Form 
{ 
    Timer t = new Timer(); 

    public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     t.Interval = 1000; 
     t.Tick += new EventHandler(t_Tick); 
     // enable timer after the handler attached 
     t.Enabled = true; 
     // Start the timer. 
     t.Start(); 
    } 
    void t_Tick(object sender, EventArgs e) 
    { 
     progressBar1.Increment(1); 
    } 
+0

-1:[将Enabled设置为true并调用Start是等同的](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.timer.enabled.aspx)。 – 2011-03-25 14:36:36

+0

是的,你是对的! – Kumar 2011-03-25 14:47:10

0

您的代码可能正在工作。问题是您的进度条上的最大值可能非常高,以至于进度条的值需要足够高才能显示下一个块。

将您的最大值设置为100,您应该看到您的代码正常工作。