Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to make a stop watch using c# windows or wpf ?
The requirements are as follows:
It must have a textbox with seconds to enter(time limit)
A start/Reset button-
A pause button

here is the screen shot
http://s28.postimg.org/sipwjwzjh/image.png[^]
Posted
Updated 17-Jun-14 20:06pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Jun-14 1:52am    
What does it mean, "windows or WPF"? :-)
You did not explain the behavior, despite the use of the word "requirements". You don't have any real requirement, so how anyone can help you.
—SA
Gouthamitks 18-Jun-14 2:01am    
I made it in windows application. But the timer control is not working correctly.
Here is the code for the windows form

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



namespace SuperTimer
{
public partial class SuperTimer : Form
{
int Rtime = 0, hours = 0, mins = 0, seconds = 0, Usertime, i, j, k, l, m, n,stop,resume;
public SuperTimer()
{
InitializeComponent();
}

private void SuperTimer_Load(object sender, EventArgs e)
{
btnpause.Enabled = false;
}


public void btnstart_Click(object sender, EventArgs e)
{
btnpause.Enabled = true;
if (txtsec.Text != string.Empty)
{
if (btnstart.Text == "Start")
{
btnstart.Text = "Reset";
Time();
Timer mytimer = new Timer();
mytimer.Tick += new EventHandler(timer_Tick);
mytimer.Interval = 1000;
mytimer.Start();
lblsec.Text = seconds.ToString();
stop = 2;
}

else
{


Reset();
return;
}
}
else
{
lbltimeup.Visible = false;
lblmsg.Text = "Please Enter Time in Seconds";
lblmsg.ForeColor = Color.Red;


}
}
private void Reset()
{
Timer mytimer = new Timer();
txtsec.Text = string.Empty;
mytimer.Stop();

lblsec.Text = "00";
lblmm.Text = "00";
lblhrs.Text = "00";
btnstart.Text = "Start";
stop = 1; seconds = 0;
mins = 0;
hours = 0;

//mytimer.Start();
}
private void Time()
{
lblmsg.Text = string.Empty;
if (txtsec.Text != string.Empty)
{
Usertime = int.Parse(txtsec.Text.ToString());
hours = Usertime / 3600;
Rtime = Usertime - (hours * 3600);
Usertime = Rtime;
mins = Usertime / 60;
Rtime = Usertime - (mins * 60);

seconds = Rtime;
Conditions();
lblhrs.Text = hours.ToString();
lblmm.Text = mins.ToString();
lblsec.Text = seconds.ToString();
}
//else
//{
// lbltimeup.Visible = false;
// lblmsg.Text = "Please Enter Time in Seconds";
// lblmsg.ForeColor = Color.Red;
//}
}
private void Conditions()
{
// if (btnstart.Text == "Start")
// {
if (seconds == 0 && mins != 0)
{
mins = mins - 1;
seconds = 59;
}
if (seconds == 0 && mins == 0)
{
hours = hours - 1;
mins = 59;
seconds = 59;
}
// }
else
return;
}


private void TimerDispalys()
{
seconds = int.Parse(lblsec.Text);

if (seconds != 0)
{
j = seconds;

for (i = 0; i <= j; i++)
{


Timerdisplay();
seconds = seconds - 1;
}


}
}
private void Timerdisplay()
{
lblsec.Text = seconds.ToString();
}

private void txtsec_TextChanged(object sender, EventArgs e)
{




}
Gouthamitks 18-Jun-14 2:08am    
The problem is when i insert time in seconds for eg.,100, and click start . Timer runs,but when i click pause and click resume.,the time is running very fast.

1 solution

i think this needs a new user control for the seconds hand and why cant you try a digital stop watch by using timer tool. even for the pointing stopwatch you need to have the timer
 
Share this answer
 

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