如何在 C# 中创建右键单击事件处理程序

Dil*_*n V 2 c# winforms

我想知道如何在右键单击窗口时显示上下文菜单。

到目前为止,这是我的代码:

private void ShowContextMenu_RightClick(object sender, EventArgs e)
{
    toolStripMenuItem5.Visible = true;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programmed by D & K");
}
Run Code Online (Sandbox Code Playgroud)

BRA*_*mel 5

AFAIK winforms 中没有直接的 RightClick 事件。您可以使用 mousedown 事件来实现此目的

  private void toolStripButton1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programed by D & K");
            }
        }
Run Code Online (Sandbox Code Playgroud)