0
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();
    }

使用 C#,为什么这个计时器不起作用?我希望能够设置一个日期,(01/01/2013)和每分钟等于三个月

4

1 回答 1

4

扩展@dbaseman 的评论,我认为您的部分问题可能在于您试图在事件侦听器中添加事件侦听器。尝试这个:

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();

或者如果你不喜欢 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();
}
于 2013-05-14T00:23:08.663 回答