Event when click on menu
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Event when click on menu
Hi again
Is it possible somehow to write an event for when a menu is clicked? I mean the "top level menu" as "File" for example. So I want to open a dialog when a specific menu is clicked.
Thanks,
Thomas
Is it possible somehow to write an event for when a menu is clicked? I mean the "top level menu" as "File" for example. So I want to open a dialog when a specific menu is clicked.
Thanks,
Thomas
Re: Event when click on menu
Please don't do this. When users click on items in the menubar, they expect a menu to open in which they can see what commands / actions are available. Immediately performing an action like opening a dialog would be quite confusing.
If you want those "direct to action" buttons, use a toolbar.
If you want those "direct to action" buttons, use a toolbar.
Use the source, Luke!
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Re: Event when click on menu
Hm, somwhere I've already read that actually you shouldn't do this. But I'd like to have an item "Search" as a menu where a little search dialog pops up. I would find it nice to have it directly in the menu as to not overload my window with buttons. And it would be cool to know how to do it just for the sake of it.
Re: Event when click on menu
Hi,
You better have submenus created under the menu bar and use them to handle user action.
Also I'm not sure I understand your comment about "overloading window with buttons". Could you please explain?
Thank you.
You better have submenus created under the menu bar and use them to handle user action.
Also I'm not sure I understand your comment about "overloading window with buttons". Could you please explain?
Thank you.
- doublemax@work
- Super wx Problem Solver
- Posts: 472
- Joined: Wed Jul 29, 2020 6:06 pm
- Location: NRW, Germany
Re: Event when click on menu
Try wxEVT_MENU_OPEN event.Wanderer82 wrote: ↑Mon Jul 26, 2021 8:57 am Hm, somwhere I've already read that actually you shouldn't do this. But I'd like to have an item "Search" as a menu where a little search dialog pops up. I would find it nice to have it directly in the menu as to not overload my window with buttons. And it would be cool to know how to do it just for the sake of it.
https://docs.wxwidgets.org/trunk/classw ... event.html
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Re: Event when click on menu
I mean that my program window is already "packed" with things so that I don't want to have another button or so for the "Search" part. I didn't mean "overload" in the programmatical sense
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Re: Event when click on menu
Okay, I managed to get it to work using EVT_MENU_OPEN, but now my function is called with every menu I click. Somehow I have to link the specific menu to the function. How can I do this? I tried using event.GetMenu() but I don't know how to use the return value for an if-statement because the return value is of wxMenu type. Could someone pleas help me with this?
Re: Event when click on menu
You have the pointer to the menu when you create it and add it to the menubar. Store it in a member variable, so you can use it for the if-check later.
Use the source, Luke!
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Re: Event when click on menu
Hm, so the first problem is, I don't know how to store the pointer in a member variable. I don't know of what kind this variable has to be.
And the second question: Is this the only way to do it? I mean, do I really have to generally use the EVT_OPEN_MENU on all menus and then put an if-check? Can't I just write en event only for this specific menu?
And the second question: Is this the only way to do it? I mean, do I really have to generally use the EVT_OPEN_MENU on all menus and then put an if-check? Can't I just write en event only for this specific menu?
Re: Event when click on menu
Hi,
Can you show the code on how you bind the event?
Thank you.
Can you show the code on how you bind the event?
Thank you.
Re: Event when click on menu
wxMenu*Wanderer82 wrote: ↑Sat Jul 31, 2021 12:57 pm Hm, so the first problem is, I don't know how to store the pointer in a member variable. I don't know of what kind this variable has to be.
No. Menu events are sent directly to the toplevel window, you can't Bind() an event handler to a specfic menu or menu item.Wanderer82 wrote: ↑Sat Jul 31, 2021 12:57 pm And the second question: Is this the only way to do it? I mean, do I really have to generally use the EVT_OPEN_MENU on all menus and then put an if-check? Can't I just write en event only for this specific menu?
Use the source, Luke!
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Re: Event when click on menu
Okay, that's a pity. Well, what values will the type "wxMenu*" return? I mean, I have to store the value somewhere in a variable and compare it, so I have to know what value can be expected to be able to compare it against something.
So I have to create a variable of typ wxMenu* and assign it the value of what exactly? Sorry, I have to admit that these things are still like a little secret to me
So I have to create a variable of typ wxMenu* and assign it the value of what exactly? Sorry, I have to admit that these things are still like a little secret to me
Re: Event when click on menu
Somewhere in your code you must have something like this (taken from 'minimal' sample):Wanderer82 wrote: ↑Sat Jul 31, 2021 5:16 pm Okay, that's a pity. Well, what values will the type "wxMenu*" return? I mean, I have to store the value somewhere in a variable and compare it, so I have to know what value can be expected to be able to compare it against something.
So I have to create a variable of typ wxMenu* and assign it the value of what exactly? Sorry, I have to admit that these things are still like a little secret to me
Code: Select all
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
Code: Select all
helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
Then, in the wxEVT_MENU_OPEN menu handler:
Code: Select all
void MyFrame::OnMenuOpen(wxMenuEvent &event)
{
if( event.GetMenu() == helpMenu )
{
wxLogDebug("help menu opened");
}
}
Use the source, Luke!
-
- Ultimate wxWidgets Guru
- Posts: 670
- Joined: Tue Jul 26, 2016 2:00 pm
Re: Event when click on menu
Hm, I actually didn't have to add a member variable. Everything was already in place but not at the place from your "minimal sample". The variable was at the beginning inside the "wxWindow part". I only had to write the if-statement, the way you showed me. Plus, I had to take the already existing variable "wxMenu *Menu3" out of its "normal" position (inside the wxWindow part) as it couldn't find the variable (not declared in this scope). Why are these variables not declared in the .h-file as for example the menu items?
Also I noticed a behavior that I didn't necessarily want: If I click on "File" and open this menu and then move the mouse over the next menu "Search", the search dialog is shown as well. Is there a way to make the dialog only show up if the user also clicks on the menu?
Also I noticed a behavior that I didn't necessarily want: If I click on "File" and open this menu and then move the mouse over the next menu "Search", the search dialog is shown as well. Is there a way to make the dialog only show up if the user also clicks on the menu?
Re: Event when click on menu
I can't think of any way to prevent this as we can't detect the mouse click and the wxEVT_MENU_OPEN event is the only information we have.Wanderer82 wrote: ↑Sat Jul 31, 2021 10:24 pm Also I noticed a behavior that I didn't necessarily want: If I click on "File" and open this menu and then move the mouse over the next menu "Search", the search dialog is shown as well. Is there a way to make the dialog only show up if the user also clicks on the menu?
Use the source, Luke!