public

Subversion Repositories:
[/] [StopWatch/] [StopWatch/] [StopWatch.cs] - Rev 6

Compare with Previous - Blame


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace StopWatch
{
    public partial class StopWatch : Form
    {
        private DateTime startTime;
        private DateTime stopTime;
        private int intLap = 1;
        private int startTickCount = 0;
        private int stopTickCount = 0;
        private TimeSpan ts;


        public StopWatch()
        {
            InitializeComponent();
        }

        [DllImport("coredll.dll")]
        static extern void SystemIdleTimerReset();

        private void btnStartStop_Click(object sender, EventArgs e)
        {
            if (btnStartStop.Text.Equals("Start"))
            {
                btnStartStop.Text = "Stop";
                btnLap.Enabled = true;
                startTime = DateTime.Now;
                startTickCount = Environment.TickCount;
                timer1.Enabled = true;
                timer2.Enabled = true;
                txtBxLaps.Text = "";
            }
            else
            {
                btnStartStop.Text = "Start";
                btnLap.Enabled = false;
                stopTime = DateTime.Now;
                stopTickCount = Environment.TickCount;
                timer1.Enabled = false;
                timer2.Enabled = false;
                ts = new TimeSpan(0,0,0,0, stopTickCount - startTickCount);
                lblTime.Text = String.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", ts.Hours , ts.Minutes, ts.Seconds, ts.Milliseconds);
                intLap = 1;
                txtBxLaps.Text += "Start Time : " + startTime.ToLongTimeString() + "\r\n";
                txtBxLaps.Text += "Stop Time : " + stopTime.ToLongTimeString() + "\r\n";
                txtBxLaps.Text += "Duration : " + String.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", ts.Hours , ts.Minutes, ts.Seconds, ts.Milliseconds) + "\r\n";
            }
            txtBxLaps.SelectionStart = txtBxLaps.Text.Length;
            txtBxLaps.ScrollToCaret();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ts = new TimeSpan(0, 0, 0, 0, Environment.TickCount - startTickCount);
            lblTime.Text = String.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", ts.Hours , ts.Minutes, ts.Seconds, ts.Milliseconds);
        }

        private void btnLap_Click(object sender, EventArgs e)
        {
            ts = new TimeSpan(0, 0, 0, 0, Environment.TickCount - startTickCount);
            txtBxLaps.Text += String.Format("Lap #{0:D2} : {1:D2}:{2:D2}:{3:D2}.{4:D3}", intLap, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds) + "\r\n";
            intLap++;
            txtBxLaps.SelectionStart = txtBxLaps.Text.Length;
            txtBxLaps.ScrollToCaret();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (Environment.OSVersion.Platform.ToString().ToLower().Equals("wince"))
            {
                SystemIdleTimerReset();
            }
        }

    }
}

Generated by GNU enscript 1.6.4.