2015-07-11 68 views
0

我想知道是否有每1000毫秒刷新進度欄的方法,因爲當我運行我的程序時,我必須單擊進度欄才能從中獲取反饋。如何在C#中每秒刷新一次字符串

private void progressBar1_Click(object sender, EventArgs e) 
    { 
     Application.EnableVisualStyles(); 
     progressBar1.Style = ProgressBarStyle.Continuous; 
     progressBar1.Value = (int)(power.BatteryLifePercent * 100); 
     label1.Text = string.Format("{0}%", (power.BatteryLifePercent * 100)); 
    } 

這是我到目前爲止。

回答

2

執行使用定時器

public void Form_Load(object sender, EventArgs e) 
{ 
    // Moved here, supposing that you don't not needed to 
    // set them to same value every second.... 
    Application.EnableVisualStyles(); 
    progressBar1.Style = ProgressBarStyle.Continuous; 

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer(); 
    t.Interval = 1000; 
    t.Tick += timeElapsed; 
    t.Start(); 
} 

private void timeElapsed(object sender, EventArgs e) 
{ 
    progressBar1.Value = (int)(power.BatteryLifePercent * 100); 
    label1.Text = string.Format("{0}%", (power.BatteryLifePercent * 100)); 
} 
+0

好的任務每隔X時間間隔通常被解決。感謝那。我會試試看,並告訴你它是否工作。 – Sachin

+0

還在等待,知道它是否已經工作 – Steve

+0

不。它沒有工作。抱歉。 – Sachin