using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace SoftwareEngineering
{
public partial class MainGame : Form
{
private int tick = 60;
public MainGame()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
}
private void MainGame_Load(object sender, EventArgs e)
{
}
private void NewGame_Click(object sender, EventArgs e)
{
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timeremaining.Text = tick + " Remaining";
if(tick > 0)
{
tick--;
}
else
{
timeremaining.Text = "Times Up!";
}
}
private void timeremaining_TextChanged(object sender, EventArgs e)
{
}
我的计时器有问题,当我启动程序时,计时器将自动倒计时而不按下按钮,如果单击按钮,计时器将减1。
实施例: “56剩余”当我点击按钮时,计时器将减少,结果将是“54剩余”,依此类推。 (单击按钮)从“54到51”。
如何解决此问题请帮忙。
答案 0 :(得分:3)
看起来您在表单设计器上使用了Timer控件并启用了它。这会导致它在加载表单后立即开始计时。把它弄错。