Visual C#表单右键单击按钮

问题描述 投票:12回答:3

我正在尝试在visual c#中制作一个扫雷型游戏,我希望在我右键单击并左键单击按钮时会发生不同的事情,我该怎么做?

我试过这段代码,但它只注册了左键点击:

    private void button1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MessageBox.Show("Left");
        }
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            MessageBox.Show("Right");
        }

    }
c# winforms click mouseevent
3个回答
8
投票

您将不得不使用MouseUpMouseDown事件而不是Click事件来捕获右键单击。


1
投票

只需尝试使用button1_MouseDown事件而不是button1_MouseClick事件。它将解决您的问题。

 private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
          //do something
        }
        if (e.Button == MouseButtons.Right)
        {
          //do something
        }
    }

0
投票

按钮只对MouseButtons.Left作出反应,而不是对MouseButton.Right反应,甚至对中间反应。

void Select(object sender, MouseEventArgs e)
{
    /* var btn = sender as CardButton;*/

    if (e.Button == MouseButtons.Left)
    {
        if (this.Selected == false)
        { 
            this.Selected = true;
        }
        else
        {
            this.Selected = false;
        }
    }
    if (e.Button == MouseButtons.Right)
    {
        if (this.Selected == false)
        {
            this.Selected = true;
        }
        else
        {
            this.Selected = false;
        }
    }

    Draw();
}
© www.soinside.com 2019 - 2024. All rights reserved.