algoBaller
algoBaller

Reputation: 59

Set Specified date 1 Minute equals 3 months

DateTime newDate = new DateTime(2013, 1, 1);  

    private void timer1_Tick(object sender, EventArgs e)  
    {  
        timer1.Tick += new EventHandler(timer1_Tick);  
         timer1.Interval = 60000;

         timer1.Enabled = true;
         timer1.Start();
         newDate.AddMonths(+3);
         lblDate.Text = newDate.ToString();
    }

Using C#, why does this timer not work? I want to be able to set a date, (01/01/2013) and for each minute that proceeds to equal three months

Upvotes: 0

Views: 102

Answers (1)

Ben Bishop
Ben Bishop

Reputation: 1414

Expanding on @dbaseman 's comment, I think part of your problem may lie in that you are trying to add an event listener inside of your event listener. Try this:

DateTime newDate = new DateTime(2013, 1, 1);
timer1.Interval = 60000;  
timer1.Enabled = true;
timer1.Tick += (object sender, EventArgs e) =>{
     newDate = newDate.AddMonths(+3);
     lblDate.Text = newDate.ToString();
}
timer1.Start();

Or if you don't like Lambdas

void Init(){
    DateTime newDate = new DateTime(2013, 1, 1);
    timer1.Interval = 60000;  
    timer1.Enabled = true;
    timer1.Tick += new EventHandler(timer1_Tick)
    timer1.Start();
}

void timer1_Tick(object sender, EventArgs e)  
{
     newDate = newDate.AddMonths(+3);
     lblDate.Text = newDate.ToString();
}

Upvotes: 4

Related Questions