简体   繁体   中英

picturebox click event fires before the picturebox's contextmenu menuitem click

I have a PictureBox.Click event and I also have a PictureBox.ContextMenu -- when I click on a menu item within the context menu, it fires the PictureBox.Click event first, then the event attached to the menu item.

This is not what I want. Is there a way to fire the menu item event only (or at least, first)?

When there is no e.Handled parameter you can use a flag to abort the Click code:

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM