每秒自动更新标签

本文关键字:更新 标签 | 更新日期: 2023-09-27 18:32:04

所以我正在尝试制作一个答题器游戏,我已经为出厂升级设置了一个计时器。饼干会更新,但在后台进行。

在您单击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;
    }
}

(中间有一些行,但它们并不重要)。

每秒自动更新标签

更新 OnTimedEvent 中的标签,您可以在其中计算 cookie。

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