【问题标题】:picturebox click event fires before the picturebox's contextmenu menuitem click图片框单击事件在图片框的上下文菜单菜单项单击之前触发
【发布时间】:2015-04-23 09:38:27
【问题描述】:

我有一个 PictureBox.Click 事件,我也有一个 PictureBox.ContextMenu —— 当我点击上下文菜单中的一个菜单项时,它首先触发 PictureBox.Click 事件,然后是附加到菜单项的事件。

这不是我想要的。有没有办法只触发菜单项事件(或至少首先触发)?

【问题讨论】:

  • 如何打开上下文菜单?

标签: c# winforms contextmenu picturebox


【解决方案1】:

当没有e.Handled 参数时,您可以使用标志来中止Click 代码:

bool flag = false;

private void pb_target_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Console.WriteLine("pb_target_MouseDown RIGHT");
        flag = true;
    }

    else flag = false;
}

private void pb_target_MouseClick(object sender, MouseEventArgs e)
{
    if (flag) { flag = false; return; }

    Console.WriteLine("pb_target_MouseClick");
}

【讨论】:

  • 很不错,没想到会卡住右键。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多