我创建了Forms.Timer的子类,我无法停止此计时器。
public class Clock : Timer
{
private int startTime;
private PictureBox one;
private PictureBox two;
private PictureBox three;
public Clock(Size _size) : base()
{ startTime = 0; initialComponent(_size); }
public Clock(int start_time, Size _size) : base()
{ startTime = start_time; initialComponent(_size); }
public Point Location
{
get { return three.Location; }
set
{
three.Location = value;
two.Location = new Point(value.X + three.Width, value.Y);
one.Location = new Point(value.X + three.Width + two.Width, value.Y);
}
}
private void initialComponent(Size _size)
{
......
}
public void Show()
{
int bits = startTime % 10;
int ten = (startTime % 100) / 10;
int hundred = (startTime % 1000) / 100;
one.Image = ImageLauncher.loadImage(bits, ImageLauncher.ImageType.Timer);
two.Image = ImageLauncher.loadImage(ten, ImageLauncher.ImageType.Timer);
three.Image = ImageLauncher.loadImage(hundred, ImageLauncher.ImageType.Timer);
}
protected override void OnTick(EventArgs e)
{
startTime++;
if (startTime > 999 || startTime < 0) return;
this.Show();
}
}
我在窗体的构造函数中创建一个对象,计时器可以正常启动,但Stop()方法不起作用。我试图创建一个方法来将Timer.Enabled设置为false,但它仍在继续,更奇怪的是我试图用timer.Enabled以同样的方法重置间隔,间隔已经改变但是计时器仍然没有消失。
在构造函数中:
clock = new Clock(new Size(23 * 3, 38));
启动:
if (!clock.Enabled) clock.Start();
带停止的方法:
private void gameOver(bool is_win)
{
clock.Stop();
showAll();
string winface = is_win ? "cool" : "sad";
ShortCutStart.Image = ImageLauncher.loadImage(winface, ImageLauncher.ImageType.Face);
ShortCutStart.Name = winface;
}