How to trigger a programmatically generated event for wxRadioButton in wxWidgets?

I am trying to programmatically change the value of wxRadioButton as the user does. Changing the value does not cause an event corresponding to the button, and this makes sense, since the documentation clearly states:

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 trigger a programmatically generated event for wxRadioButton?

I assume this has something to do with:

 wxWindow window->AddPendingEvent(wxEvent *event ) 

Nice to appreciate a simple example.

+4
source share
2 answers

You can use AddPendingEvent or ProcessEvent (process 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 (?).

+2
source

While the above may work in this case, it does not guarantee work for all controls (and really does not work with many controls), and therefore the recommended way to do what you want, i.e., I think, call your own handler for this event is to extract the code of the event handler into a separate function that you can simply call. for instance

 class MyFrame { ... void DoHandleRadioButton() { /* your code here */ } void OnRadioButton(wxCommandEvent& event) { DoHandleRadioButton(); } }; 

and then just call DoHandleRadioButton() .

+1
source

Source: https://habr.com/ru/post/1305560/


All Articles