Javier García
Javier García

Reputation: 1114

C# - Trigger "right click" event before context menu

What I want to do is to select an item within a listbox when I rightclick it just before the contextmenustrip shows.

I have seen that the order is otherwise: the context menu is popped up first and once disposed, the rightclick event triggers.

This is my listbox (with its associated context menu): https://i.gyazo.com/b2b2d7d5f8094db9c7e62565df2cafb9.png

This would be my rightclick event:

private void listBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);
        }
    }

I have seen many posts, but none really explain how to manage the order of events triggering.

Upvotes: 2

Views: 1339

Answers (1)

Javier García
Javier García

Reputation: 1114

By @Panagiotis Kanavos: "Use the MouseDown, not the MouseClick event. A MouseClick event is raised when both MouseDown and MouseUp events are received by your application."

This just did the trick :)

Upvotes: 2

Related Questions