0

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

But the problem is:

只有当我释放键时才会调用 click 事件。

What I need:

当我按住键时单击事件应该触发,当我释放键时应该停止。表示类似向上和向下滚动按钮。

4

3 回答 3

1

不要使用点击事件。使用 MouseDown 和 MouseUp 事件。

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

于 2012-04-17T05:59:33.250 回答
1

例如,在按钮的 MouseDown 事件中更改某些类级别成员

blnButtonPressed = ture;

关于按钮更改的 MouseUp 事件

blnButtonPressed = false;

做你在这两种状态之间所做的一切......

于 2012-04-17T06:03:06.753 回答
0

您可以创建自定义按钮,当它被按下时会引发 Click 事件。这是执行此操作的简单方法:

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 事件。

要使用它,只需编译项目并将 PressedButton 从 ToolBox 拖到您的表单中。

于 2012-04-17T06:57:40.820 回答