按钮应该像上下滚动一样工作

本文关键字:一样 工作 滚动 上下 按钮 | 更新日期: 2023-09-27 18:31:31

我有一个Windows应用程序,其中有两个按钮,用于在网格视图中上下移动项目。

But the problem is:

仅当我释放密钥时,才会调用 click 事件。

What I need:

单击事件应在我按住该键时触发,在我松开该键时应停止。 表示类似于向上和向下滚动按钮的内容。

按钮应该像上下滚动一样工作

在按钮的鼠标按下事件上,例如更改某些类级别成员

blnButtonPressed = ture;

在按钮更改的鼠标向上事件时

blnButtonPressed = false;

在这两个州之间做任何你做的事情......

不要使用 click 事件。 使用鼠标按下和鼠标打开事件。

或者,如果要处理按键,请使用 KeyDown 和 KeyUp 事件。

您可以创建自定义按钮,该按钮在按下时将提高单击偶数。这是执行此操作的简单方法:

public class PressableButton : Button
{
    private Timer _timer = new Timer() { Interval = 10 };
    public PressableButton()
    {
        _timer.Tick += new EventHandler(Timer_Tick);
    }
    private void Timer_Tick(object sender, EventArgs e)
    {
        OnClick(EventArgs.Empty);
    }
    protected override void OnMouseDown(MouseEventArgs mevent)
    {
        base.OnMouseDown(mevent);
        _timer.Start();
    }
    protected override void OnMouseUp(MouseEventArgs mevent)
    {
        base.OnMouseUp(mevent);
        _timer.Stop();
    }      
}

按下按钮后,计时器每 10 毫秒开始滴答一次(您可以更改间隔)。在计时器时钟周期事件处理程序上,此按钮将引发 Clieck 事件。

要使用它,只需编译项目并将按下按钮从工具箱拖到您的表单中。