Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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");
        }
    }
}
Posted
Updated 4-Feb-14 9:17am
v5

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:
C#
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:
C#
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;
    }
 
Share this answer
 
Comments
omar-ahmed99 5-Feb-14 5:12am    
what about XAML controls?
OriginalGriff
OriginalGriff 5-Feb-14 5:23am    
What about them?
omar-ahmed99 5-Feb-14 5:55am    
only Two buttons
OriginalGriff 5-Feb-14 6:09am    
Yes...
omar-ahmed99 5-Feb-14 6:30am    
OK no errors in the code but it can not view the clock
i want to stop it after 30 seconds
The Stopwatch class is not the best class to handle elapsed events. Use a System.Timer.Timer and subscribe to the Elapsed event.
 
Share this answer
 
Comments
omar-ahmed99 5-Feb-14 4:49am    
how can i use System.Timer.Timer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900