Jakub Czaplicki
Jakub Czaplicki

Reputation: 1847

How to call an programmatically generated event for wxRadioButton in wxWidgets?

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.

Upvotes: 4

Views: 4369

Answers (2)

VZ.
VZ.

Reputation: 22753

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().

Upvotes: 2

UncleBens
UncleBens

Reputation: 41351

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(?).

Upvotes: 2

Related Questions