如何在10秒后停止时钟(示例)像国际象棋时钟? [英] How Can I Stop Clock After 10 Seconds (Example) Like Chess Clock?

查看:123
本文介绍了如何在10秒后停止时钟(示例)像国际象棋时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stopwatch ss = new Stopwatch();

public MainWindow()
{
    InitializeComponent();
}

private void button2_Click(object sender, RoutedEventArgs e)
{
    ss.Reset();
    MessageBox.Show("CLICK START BUTTON", "END");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    ss.Start();
    MessageBox.Show("Play Now","GO");
    listBox1.Items.Add(String.Format("{0:00} : {1:00} :
    {2:00}",ss.Elapsed.Hours, ss.Elapsed.Minutes, ss.Elapsed.Seconds));
    DateTime m = DateTime.Now;
    DateTime n = DateTime.Now.AddSeconds(10);
    int l = 5;
    for (int i = 0; i <= l; i++)
    {
        l++;
        if (m == n)
        {
            ss.Stop();
            MessageBox.Show("end time", "Change Player");
        }
        else
        {
            MessageBox.Show("Continue");
        }
    }
}

推荐答案

国际象棋时钟稍微复杂,但也不算太糟糕:它由两个不能同时运行的时钟显示器和一个在它们之间切换的开关组成。

因此要制作国际象棋时钟,你需要两个标签,两个按钮和两个经过的时间指标。

就个人而言,我不同意Abhinav,并选择一对秒表 - 每个玩家一个,但添加了一个计时器以显示当前值。

秒表有两个重要方法:开始和停止 - 重要的是要注意这些都不会影响秒表中的实际值 - 它们只是停止并开始更改。

所以如果你有一个更新两个显示器的计时器:

A Chess clock is slightly complex, but not too bad: it consists of two clock displays which can't run together and a switch to toggle between them.
So to make a chess clock, you need two labels, two buttons, and two Elapsed time indicators.
Personally, I would disagree with Abhinav, and go with a pair of stopwatches - one for each player, but with a timer added to display the current values.
A Stopwatch has two important methods here: Start and Stop - and it's important to note that neither of these affect the actual value in the Stopwatch - they just stop and start it changing.
So if you have a Timer which updates the two displays:
private Stopwatch blackTimer = new Stopwatch();
private Stopwatch whiteTimer = new Stopwatch();
void tDisplay_Tick(object sender, EventArgs e)
    {
    labBlack.Text = ((double)blackTimer.ElapsedMilliseconds / 1000.0).ToString("0.0");
    labWhite.Text = ((double)whiteTimer.ElapsedMilliseconds / 1000.0).ToString("0.0");
    }



然后您只需要两个简单的按钮处理程序:


Then all you need are two simple button handlers:

private void butBlack_Click(object sender, EventArgs e)
    {
    butBlack.Enabled = false;
    whiteTimer.Stop();
    blackTimer.Start();
    butWhite.Enabled = true;
    }

private void butWhite_Click(object sender, EventArgs e)
    {
    butWhite.Enabled = false;
    blackTimer.Stop();
    whiteTimer.Start();
    butBlack.Enabled = true;
    }


秒表类不是最好处理的类已过去的事件。使用 System.Timer.Timer 并订阅 Elapsed 事件。
The Stopwatch class is not the best class to handle elapsed events. Use a System.Timer.Timer and subscribe to the Elapsed event.


这篇关于如何在10秒后停止时钟(示例)像国际象棋时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆