Best way to do right-click context menu

There are plenty of examples for right-click popup context menus, but the differences in the examples leave me with two questions:

  1. When would you use wx.EVT_CONTEXT_MENU instead of wx.EVT_RIGHT_DOWN?

  2. When using a context menu with a listbox, how do you get the listbox to select the item and pop up the context menu in one click?

Relating to the second question, if I add an event.Skip() the listbox item gets selected, but only after the context menu. This means that the
previously selected listbox item gets returned as selected, while the listbox is updated to show the new selection.

Another approach is to use a wx.CallAfter to activate the popup menu from the EVT_RIGHT_DOWN event handler. This allows the listbox
item to be properly selected, but the popup menu only displays while the mouse key is held down.

A third approach is to bind to wx.EVT_RIGHT_UP for the popup context menu. This works exactly as expected, with the proper listbox
item selection and the popup menu stays on the screen. The only problem is that the popup appears only after the right mouse key is
released, which is not consistent with normal context menus.

In summary, what I want is this:

  1. A listbox or similar control that displays a variable number of items.
  2. Right click on one of the items and a context menu pops up, allowing the user to initiate action on that item.
    It’s simple, normal, and commonly implemented. Obviously I am missing some detail.

Thanks for the help,

Tom

There are plenty of examples for right-click popup context menus, but
the differences in the examples leave me with two questions:

1) When would you use wx.EVT_CONTEXT_MENU instead of wx.EVT_RIGHT_DOWN?

Whenever you want your program to match the platform's default behavior. Some send the context event on RIGHT_DOWN, some send it on RIGHT_UP, and don't forget about the context menu key on the keyboard for the Windows platform.

2) When using a context menu with a listbox, how do you get the listbox
to select the item and pop up the context menu in one click?

The proper behavior here can also be platform dependent, however if you use EVT_CONTEXT_MENU then I think that if the right-click is supposed to select an item for that platform then it will have done so by the time the context menu event is delivered.

OTOH, if you want to things to work a certain way and not necessarily follow the platform conventions, then you can do things yourself with the mouse right-click events. You'll probably want to use the listbox's HitTest method to get the item under the mouse cursor and then programmatically select it instead of trying to wait until after the listbox has selected it on its own.

···

On 11/13/12 7:07 AM, qbee42 wrote:

--
Robin Dunn
Software Craftsman

Thanks, Robin. The HitTest method did the trick. That way the event sequence became irrelevant.

···

On Tuesday, November 13, 2012 10:07:39 AM UTC-5, qbee42 wrote:

There are plenty of examples for right-click popup context menus, but the differences in the examples leave me with two questions:

  1. When would you use wx.EVT_CONTEXT_MENU instead of wx.EVT_RIGHT_DOWN?

  2. When using a context menu with a listbox, how do you get the listbox to select the item and pop up the context menu in one click?

Relating to the second question, if I add an event.Skip() the listbox item gets selected, but only after the context menu. This means that the
previously selected listbox item gets returned as selected, while the listbox is updated to show the new selection.

Another approach is to use a wx.CallAfter to activate the popup menu from the EVT_RIGHT_DOWN event handler. This allows the listbox
item to be properly selected, but the popup menu only displays while the mouse key is held down.

A third approach is to bind to wx.EVT_RIGHT_UP for the popup context menu. This works exactly as expected, with the proper listbox
item selection and the popup menu stays on the screen. The only problem is that the popup appears only after the right mouse key is
released, which is not consistent with normal context menus.

In summary, what I want is this:

  1. A listbox or similar control that displays a variable number of items.
  2. Right click on one of the items and a context menu pops up, allowing the user to initiate action on that item.
    It’s simple, normal, and commonly implemented. Obviously I am missing some detail.

Thanks for the help,

Tom