在MouseDown事件中启动的计时器不执行Tick功能

时间:2016-04-20 13:05:27

标签: c# timer mousedown

我想构建一个在C#中保存Button的函数。目前它看起来像这样:

private Timer timer1 = new Timer();

private void cmd_haken_auf_ab_MouseDown(object sender, MouseEventArgs e)
{
    Console.WriteLine("works");
    timer1.Enabled = true;
    timer1.Start();

}

private void cmd_haken_auf_ab_MouseUp(object sender, MouseEventArgs e)
{
    Console.WriteLine("works too");
    timer1.Stop();
}


private void timer1_Tick(object sender, EventArgs e)
{
    Console.WriteLine("works as well");
    cmd_p4.Height += 1;

}

但显然我的timer1_Tick函数永远不会被执行,因为输出是:

works
works too
works
works too

2 个答案:

答案 0 :(得分:0)

为您的计时器提及计时器tick_event并添加计时器interval

timer1.Interval = 1000; timer1.Tick+=timer1_Tick;

答案 1 :(得分:0)

添加构造函数:

timer1.Tick += new timer1_Tick(TimerEventProcessor);

这应该导致调用计时器的每个滴答。如果不起作用,请设置计时器的间隔,如

timer1.Interval = 1; // 1ms interval