Timer not beginning from 00:00
Posted
by
studentProgrammer
on Stack Overflow
See other posts from Stack Overflow
or by studentProgrammer
Published on 2012-12-09T10:34:00Z
Indexed on
2012/12/09
11:07 UTC
Read the original article
Hit count: 134
I have a Game in which there is a timer involved. For each two minutes that passes a new round between different players begins. So, I have a text box starting from 00:00 and changes each second until it is equal to 02:00.
Now, I want to save the state of the game in the middle of a round if the user closes the form. What I need to do is that upon loading, the textbox starts at the time that the user left the game the last time and continue up till 02:00 normally.
How can I do this?
This is what I have until now where Tournament is the Form
public Tournament()
{
_timer = new System.Windows.Forms.Timer();
_timer.Interval = 1000;
_timer.Tick += Timer_Tick;
_myDateTime = DateTime.Now;
newDate = new DateTime();
newDate = newDate.AddMinutes(2.00);
_timer.Start();
InitializeComponent();
}
void Timer_Tick(object sender, EventArgs e)
{
var diff = DateTime.Now.Subtract(_myDateTime);
this.textBox1.Text = diff.ToString(@"mm\:ss");
DateTime dt = Convert.ToDateTime(diff.ToString());
if (newDate.Minute == dt.Minute)
{
_timer.Stop();
_myDateTime = DateTime.Now;
displayPointsOrResults();
this.textBox1.Text = diff.ToString(@"mm\:ss");
}
}
In my LoadGame method: where timePassed is what I have written in the text box
string[] splitted6 = timePassed.Split(':');
if (splitted6[0] == "00")
{
int remainingTime = 120 - Convert.ToInt32(splitted6[1]);
DateTime time = DateTime.Now.Date;
time = time.AddMinutes(remainingTime);
_myDateTime = time;
}
else
{
int leftTime = Convert.ToInt32(splitted[0].Trim('0') + splitted[1]);
int remainingTime = 120 - leftTime;
DateTime time = DateTime.Now.Date;
time = time.AddMinutes(remainingTime);
_myDateTime = time;
}
© Stack Overflow or respective owner