6/30/2019

Stopwatch C# windows form

Stopwatch
Stopwatch Design Foorm

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace StopwatchProject
{
    public partial class frmStopwatch : Form
    {
        private Stopwatch mainStopwatch;
        private Stopwatch smallStopwatch; // New stopwatch for small timer  
        private Timer mainTimer;
        private Timer smallTimer; // New timer for small stopwatch  
        private int lapCount;
        private TimeSpan lastLapTime;
        public frmStopwatch()
        {
            InitializeComponent();
            mainStopwatch = new Stopwatch();
            smallStopwatch = new Stopwatch(); // Initialize small stopwatch  
            mainTimer = new Timer();
            smallTimer = new Timer(); // Initialize small timer  
            mainTimer.Interval = 100; // Update every 100ms  
            smallTimer.Interval = 100; // Update every 100ms for small timer  
            mainTimer.Tick += MainTimer_Tick;
            smallTimer.Tick += SmallTimer_Tick; // Event for small timer  
            lblTime.Text = "00:00:00.00"; // Initial time for main stopwatch  
            lblSmallTime.Text = "00:00:00.00"; // Initial time for small stopwatch  
            lapCount = 0; // Initialize lap count  
            lblLapCount.Text = "Lap Count: 0"; // Initialize lap count display  
            lastLapTime = TimeSpan.Zero; // Initialize last lap time 

            // Set up DataGridView columns  
            dataGridViewLaps.Columns.Add("LapNumber", "Lap");
            dataGridViewLaps.Columns.Add("LapTime", "Lap Times");
            dataGridViewLaps.Columns.Add("OverallTime", "Overall Time");
            dataGridViewLaps.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridViewLaps.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridViewLaps.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dataGridViewLaps.MultiSelect = false;
            dataGridViewLaps.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            dataGridViewLaps.AllowUserToResizeRows = false;
            
        }
        private void MainTimer_Tick(object sender, EventArgs e)
        {
            lblTime.Text = FormatTime(mainStopwatch.Elapsed);
        }

        private void SmallTimer_Tick(object sender, EventArgs e)
        {
            lblSmallTime.Text = FormatTime(smallStopwatch.Elapsed); // Update small stopwatch display  
        }
        private string FormatTime(TimeSpan timeSpan)
        {
            return string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                timeSpan.Hours,
                timeSpan.Minutes,
                timeSpan.Seconds,
                timeSpan.Milliseconds / 10);
        }

        private void btnStartStop_Click(object sender, EventArgs e)
        {
            if (mainStopwatch.IsRunning)
            {
                mainStopwatch.Stop();
                mainTimer.Stop();
                smallStopwatch.Stop();
                smallTimer.Stop();
                btnStartStop.Text = "Start";
            }
            else
            {
                mainStopwatch.Start();
                mainTimer.Start();
                smallStopwatch.Start();
                smallTimer.Start();
                btnStartStop.Text = "Stop";
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            mainStopwatch.Reset();
            lblTime.Text = "00:00:00.00";
            dataGridViewLaps.Rows.Clear(); // Clear the DataGridView
            lapCount = 0; // Reset lap count  
            lblLapCount.Text = "Lap Count: 0"; // Update lap count display  
            btnStartStop.Text = "Start";
            mainTimer.Stop();
            lastLapTime = TimeSpan.Zero; // Reset last lap time  

            // Reset small stopwatch  
            smallStopwatch.Reset();
            lblSmallTime.Text = "00:00:00.00"; // Reset small stopwatch display  
            smallTimer.Stop(); // Stop small timer   
            lblSmallTime.Visible = false;
        }

        private void btnLap_Click(object sender, EventArgs e)
        {
            if (mainStopwatch.IsRunning)
            {
                lapCount++; // Increment lap count  
                TimeSpan currentLapTime = mainStopwatch.Elapsed - lastLapTime; // Calculate lap time  
                lastLapTime = mainStopwatch.Elapsed; // Update last lap time  
                string lapTime = FormatTime(currentLapTime); // Get the formatted lap time  
                string overallTime = FormatTime(mainStopwatch.Elapsed); // Get the overall time  

                // Add a new row to the DataGridView  
                dataGridViewLaps.Rows.Insert(0, lapCount, lapTime, overallTime);

                dataGridViewLaps.ClearSelection();

                lblLapCount.Text = $"Lap Count: {lapCount}"; // Update lap count display  

                // Reset and start the small stopwatch  
                smallStopwatch.Reset();
                smallStopwatch.Start();
                smallTimer.Start(); // Start small timer  
                lblSmallTime.Visible=true;
            }
        }
    }
}

No comments:

Post a Comment