三键鼠标点击C#?

Jav*_*ram 9 c# mouseevent winforms

MS-Word 鼠标单击事件中用作:

单击 - 放置光标
双击 - 选择单词
三击 - 选择段落

在C#中,我可以处理单点击和双点击鼠标事件,但我想Triple Mouse ClickC#Windows TextBox中处理事件.

例:

void textbox1_TripleClick()
{
    MessageBox.Show("Triple Clicked"); 
} 
Run Code Online (Sandbox Code Playgroud)

Abu*_*buh 7

看看这个: Mousebuttoneventargs.clickcount

我认为这应该涵盖它.

  • ...除了它在Winforms中不起作用.至少在TextBox中,ClickCount只返回1 ...即使双击也是如此! (4认同)

小智 5

做这个:

    private int _clicks = 0;
    private System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
    private void txtTextMessage_MouseUp(object sender, MouseEventArgs e)
    {
        _timer.Stop();
        _clicks++;
        if (_clicks == 3)
        {
            // this means the trip click happened - do something
            txtTextMessage.SelectAll();
            _clicks = 0;
        }
        if (_clicks < 3)
        {
            _timer.Interval = 500;
            _timer.Start();
            _timer.Tick += (s, t) =>
            {
                _timer.Stop();
                _clicks = 0;
            };
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢直接插入并运行的代码。+1。谢谢! (3认同)