Finding a wxMenu's Selected Radio Item

2019-09-16 09:51发布

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.

2条回答
乱世女痞
2楼-- · 2019-09-16 09:55

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 call IsChecked() 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 static wxMenuBar method, probably), please don't hesitate to send patches/pull requests doing it!

查看更多
爷的心禁止访问
3楼-- · 2019-09-16 10:13

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:

    ResolMenu = new wxMenu();
    ResolMenu->AppendRadioItem(idRcvLoRez,"Low Resolution");
    ResolMenu->AppendRadioItem(idRcvMeRez,"Medium Resolution");
    ResolMenu->AppendRadioItem(idRcvHiRez,"High Resolution");
    ResolMenu->Check( idRcvLoRez, true );
    Bind(wxEVT_MENU,&cFrame::onRcvRez,this,idRcvLoRez);
    Bind(wxEVT_MENU,&cFrame::onRcvRez,this,idRcvMeRez);
    Bind(wxEVT_MENU,&cFrame::onRcvRez,this,idRcvHiRez);


void onRcvRez( wxCommandEvent& event )
{
    myRezID = event.GetId();
查看更多
登录 后发表回答