I'm having trouble making my stopwatch, I do not know if someone can help me do it, I'm new to this and it's a bit difficult for me.
I'm having trouble making my stopwatch, I do not know if someone can help me do it, I'm new to this and it's a bit difficult for me.
using System; using System.Globalization; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public class Chronometer { private Timer Time {get; set; } public Double Result {get; set; }
public Cronometro()
{
Tiempo.Tick += new EventHandler(Tiempo_Tick);
Tiempo.Interval = 100;
}
void Tiempo_Tick(object sender, EventArgs e)
{
Result++;
//cada tick representa 100 milisegundos
}
public void Start()
{
Tiempo.Start();
}
public void Pause()
{
//
}
public void Stop()
{
Tiempo.Stop();
}
public override string ToString()
{
return string.Format(
CultureInfo.CurrentCulture,
"{0} ms",
Result);
}
}
} and then:
using System; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1: Form { public Chronometer Chronometer {get; set; }
public Form1()
{
InitializeComponent();
Cronometro = new Cronometro();
}
private void btnStart_Click(object sender, EventArgs e)
{
Cronometro.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
Cronometro.Stop();
}
}
} To show the result, use the method Chronometer.ToString ();