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

时间:2012-04-17 05:55:49

标签: c# windows-applications

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

But the problem is:

只有在我释放密钥时才会调用click事件。

What I need:

当我按住键时,点击事件应该触发,当我释放键时应该停止。意思是像向上和向下滚动按钮。

3 个答案:

答案 0 :(得分:1)

请勿使用click事件。使用MouseDown和MouseUp事件。

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

答案 1 :(得分:1)

在按钮的MouseDown事件上更改某些类级别成员

blnButtonPressed = ture;

在按钮更改的MouseUp事件上

blnButtonPressed = false;

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

答案 2 :(得分:0)

您可以创建自定义按钮,按下时会提高Click evens。这是一种简单的方法:

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拖到您的表单。

相关问题