I'm new to C# and I'm trying to make a custom chronometer that is composed of two labels (label1 and label2) that display time strings (time and time0/time1) and one button (pause/play) that changes its text from pause to play and viceversa on each click. Label1 shows time that is a string var maked by datetime.now (hhmmss), label2 shows time0 and after clicking on the button "pause" and again on "play" it will shows time1 (time 1 is calculated by the formula below).
It does the following:
To calculate time1 I would like to use this formula:
time1 = DateTime.Now - ((difference between DateTime.Now and time0) - 1 second)
I'm stucked on the 3rd point because I don't know how to do the time difference between two strings and use the new time time1 as text for label2 and next clicks.
This is my actual code, any help to complete it is appreciate, thank you.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//time0
public int hh = 0;
public int mm = 0;
public int ss = 0;
//time
public string time = "";
public string time0 = "";
public bool IsPause = true;
public Timer t = new Timer();
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//timer interval
t.Interval = 1000; //in millisecondi
t.Tick += new EventHandler(this.t_Tick);
//start timer form loads
t.Start(); //questo userà il metodo t_Tick()
}
//timer eventhandler
private void t_Tick(object sender, EventArgs e)
{
//get current time
hh = DateTime.Now.Hour;
mm = DateTime.Now.Minute;
ss = DateTime.Now.Second;
//padding leading zero
if(hh < 10)
{
time += "0" + hh;
}
else
{
time += hh;
}
time += ":";
if(mm < 10)
{
time += "0" + mm;
}
else
{
time += mm;
}
time += ":";
if (ss < 10)
{
time += "0" + ss;
}
else
{
time += ss;
}
//update labels
label1.Text = time;
if (IsPause == false) label2.Text = time0;
else label2.Text = time;
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "pause")
{
IsPause = false;
button1.Text = "play";
time0 = label1.Text;
}
else
{
IsPause = true;
button1.Text = "pause";
}
}
}
}
It sounds as if you are best saving the time in the controls as well as the time as a string. The Tag property is there for that purpose. See https://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag%28v=vs.110%29.aspx
So, for example, if you set the DateTime you are using into label2.Tag to the same time as you format it as text in label2.Text then you can refer to it as a DateTime. Then when you need to calculate from it you can use
DateTime.Subtract - see https://msdn.microsoft.com/en-us/library/8ysw4sby%28v=vs.110%29.aspx
to determine the elapsed time.
So, to refer this to your code, wherever you have code like this, where time is the string you have created from a DateTime instance:
label1.Text = time;
you also need to set the time like this (DateTime.Now is an example, you should chose whatever you used to format the time string):
label1.Tag = DateTime.Now
Then later, when you want to know the time in label1, do this:
DateTime t = (DateTime)label1.Tag