2016-01-27 139 views
0

所以我試圖做一個答題器遊戲,並且我爲Factory升級設置了一個計時器。 Cookie更新,但在後臺。每秒自動更新標籤

標籤不會更新,直到您點擊Main按鈕(以提高cookie數量)。我試過CookieCount.Refresh()Refresh()CookieCount是我想每秒更新一次的標籤)。

public Form1() 
{ 
    InitializeComponent(); 

    TRIPLECBTN.Hide(); 

    // Create a timer with a ten second interval. 
    aTimer = new System.Timers.Timer(10000); 

    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 

    // Set the Interval to 2 seconds (2000 milliseconds). 
    aTimer.Interval = 1000; 
    aTimer.Enabled = false; 
} 
public void OnTimedEvent(object source, ElapsedEventArgs e) 
{ 
    cookies = cookies + 1; 
} 
public void button3_Click(object sender, EventArgs e) 
{ 
    if (cookies >= 1) 
    { 
     FACTORYBTN.Hide(); 
     INFO1.Hide(); 
     COST1.Hide(); 
     PRICE1.Hide(); 
     cookies -= 250; 
     MessageBox.Show("You have bought Factory!"); 
     aTimer.Enabled = true; 

    } 
} 

(有一些行之間,但他們並不重要)。

+2

WinForms應使用WinForms計時器。 – LarsTech

+0

你究竟想要完成什麼?什麼工作,什麼不工作? –

+0

除非您單擊增加cookie數量的按鈕,否則每次添加cookie時標籤都不會更新(每秒)。但它確實在後臺計數。 – byIcee

回答

0

更新OnTimedEvent中的標籤,您可以在其中計數您的Cookie。

public void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     cookies = cookies + 1; 
     label.Text = YourText 
    } 
+0

謝謝!沒有想到:s – byIcee

+0

沒問題。如果您沒有更多問題,請將其標記爲答案 – Schlinger