Let's say that I have a group of radio items in a wxMenu
. I know that exactly one of these will be checked at any given time.
Does the wxMenu
or some other construct hold onto the index of the checked item, or do I need to call the isChecked
on each radio item till I find the checked element to find it's index?
I've asked this question
about how to do that, but I'd much prefer wxWidgets saved me from doing that everywhere.
No, saving the index of the last selected item (as shown in ravenspoint's answer) or using
wxMenuBarBase::IsChecked()
until you find the selected radio button is the only way to do it.For wxWidgets to provide access to the currently selected button it would need not only to store it (which means not forgetting to update not only when the selected changes, but also when items are inserted into/deleted from the menu, so it's already not completely trivial), but to somehow provide access to the radio items group you're interested in, which would require being able to identify it and currently there is no way to do it and adding it isn't going to be particularly simple.
What could be done easily, however, is writing a reusable function
int GetIndexOfSelectedRadioItem(int firstItem)
that would start at the given item and callIsChecked()
on subsequent items until it returns true and return the offset of the item. You should be able to do it in your own code, but if you'd like to include such function in wxWidgets itself (as a staticwxMenuBar
method, probably), please don't hesitate to send patches/pull requests doing it!It is easy enough to roll your own.
Bind an event handler to wxEVT_COMMAND_RADIOBUTTON_SELECTED for every button. In the handler, extract the ID of the selected radio button and store it somewhere.
Like this: