views:

87

answers:

2

I am trying to programmatically change a value of a wxRadioButton in a way the user would do it. A value change doesn't call the event corresponded to the button, and it make sense since the documentation says it clearly:

wxRadioButton::SetValue
void SetValue(const bool value)
Sets the radio button to selected or deselected status.
This does not cause a wxEVT_COMMAND_RADIOBUTTON_SELECTED event to get emitted.

So the question is how can I call an programmatically generated event for a wxRadioButton ?

I guess that it's something to do with:

wxWindow window->AddPendingEvent(wxEvent *event )

A simple example would be greatly appreciated.

+2  A: 

You can use AddPendingEvent or ProcessEvent (handle immediately).

 bttn->SetValue(true);
 wxCommandEvent ev(wxEVT_COMMAND_RADIOBUTTON_SELECTED, id_button);
 bttn->GetEventHandler()->ProcessEvent(ev);

It should also be possible to use wxControl::Command, but it seems to me that SetValue should be called after that(?).

UncleBens
Thanks. It works. I was going to paste my code, but it seems that the comment doesn't have this nice feature of code viewing. Jakub
Jakub Czaplicki
A: 

While the above might work in this case, it's not guaranteed to work for all controls (and indeed doesn't work with many controls) and so the recommended way to do what you want, i.e., I guess, invoke your own handler for this event, is to extract the event handler code into a separate function which you may simply call. For example

class MyFrame {
...
    void DoHandleRadioButton() { /* your code here */ }

    void OnRadioButton(wxCommandEvent& event) { DoHandleRadioButton(); }
};

and then simply call DoHandleRadioButton().

VZ