Page 1 of 1

MyTextCtrl context menu is followed by a default pop menu

Posted: Thu Mar 14, 2019 8:57 pm
by kathbeau
I have a wxScrolledWindow, with a wxBoxSizer.

I create a series of custom wxBoxSizer objects, each with multiple controls, and set these into the scrolled window sizer. Each of these custom objects represents a data item, including its structure, rules and current value.

One of the controls on the custom sizers is MyTextCtrl, which handles such things as changing font color when the text value is changed by the user.

I added a context menu to MyTextCtrl so that I can, among other things, revert the modified value back to its original state. All this is working as I want it to.

However, when I invoke the context menu, after my menu runs it is followed by what I presume to be a default context menu for wxTextCtrl (it contains items Undo, Cut, Copy, Paste, etc.).

Here is my code for MyTextCtrl:

Code: Select all

// in the .h

class TXMLDataForm;
class TItemDef;

	POP_RESTORE = 3000,

class MyTextCtrl : public wxTextCtrl
	TXMLDataForm *FDataForm;
	TItemDef     *FItemDef;
	MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value,
		const wxPoint &pos, const wxSize &size, int style = 0)
		: wxTextCtrl(parent, id, value, pos, size, style)
		FDataForm = NULL;
		FItemDef = NULL;

	void SetDataForm(TXMLDataForm* data_form) { FDataForm = data_form; }
	void SetItemDef(TItemDef* item_def) { FItemDef = item_def; }

	void OnExitTextCtrl(wxFocusEvent& event); // for FValueText on DisplayDataSizer
	void OnPopupMenu(wxContextMenuEvent& event);
	void OnEvent(wxMouseEvent& event);

	void OnRestoreOriginal(wxCommandEvent& event);


// in the .cpp
wxBEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
	EVT_MENU(POP_RESTORE, MyTextCtrl::OnRestoreOriginal)  // handler for pop menu option 1

void MyTextCtrl::OnPopupMenu(wxContextMenuEvent& event)
	wxMenu menu;
	menu.Append(POP_RESTORE, "&Restore original value");
	menu.Append(POP_MOVE_TO_TOP, "Move item to top of display");
	menu.Append(POP_SHOW_DEF, "Show dictionary definition");
	PopupMenu(&menu, ScreenToClient(event.GetPosition()));


void MyTextCtrl::OnEvent(wxMouseEvent& event)

void MyTextCtrl::OnRestoreOriginal(wxCommandEvent& event)
	wxString t_value = this->GetValue();
	if (FDataForm->RestoreOriginalValue(FItemDef, t_value))


How can I make the default context menu not pop up after my custom one? I have seen messages here that talk about implementing a custom event handler (i.e., for EVT_MOUSE_EVENTS), but I can't figure out how to use that

This is wxWidgets 3.1.1 on Win10-64, working in MS Visual Studio 2017 Community.


Re: MyTextCtrl context menu is followed by a default pop menu

Posted: Thu Mar 14, 2019 9:14 pm
by doublemax
Don't call event.Skip() in OnPopupMenu().

If you don't call it, it means that you have "consumed" the event and the event handling will stop there.
But calling it means that other event handlers for the same event will be called.

And yes, the naming of the method doesn't make very clear what it does.

Re: MyTextCtrl context menu is followed by a default pop menu

Posted: Thu Mar 14, 2019 10:29 pm
by kathbeau
Don't call event.Skip() in OnPopupMenu().
Oh good grief. I could have sworn I tried that. Thank you!