2011-03-25 55 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,您應該看到您的代碼正常工作。