Long pressed button

后端 未结 5 2212
轮回少年
轮回少年 2021-01-18 13:15

I want to repeat an action when a Button is pressed during a long time, like for example the forward button of an MP3 reader. Is there an existing c# event in W

5条回答
  •  走了就别回头了
    2021-01-18 14:00

    UPDATED: Shortest way:

    Using Anonymous Methods and Object Initializer:

    public void Repeater(Button btn, int interval)
    {
        var timer = new Timer {Interval = interval};
        timer.Tick += (sender, e) => DoProgress();
        btn.MouseDown += (sender, e) => timer.Start();
        btn.MouseUp += (sender, e) => timer.Stop();
        btn.Disposed += (sender, e) =>
                            {
                                timer.Stop();
                                timer.Dispose();
                            };
    }
    

提交回复
热议问题