【问题标题】: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");
}