dimanche 27 septembre 2020

wxWidgets pprogrammaticaly move to next input control

I originally had code that set the focus to the first widget in a dialog, in the onInit method. But there were problems with it: if I pressed TAB, indeed focus moved to next control (wxTextCtrl), which got the blue 'focus' color, but the 'focus' color/highlight was not removed from previous focus widget. So now it looked like both first and second control had focus at the same time...

When cycling manually (by pressing TAB) full circle (till last control and then wrap around to the first), suddenly all worked well. That is, when moving focus from first control to next one, the first visually lost focus (blue color was removed) as it should. From now on, only one item had the focus color/highlight.

So instead of setting focus on the first control, I tried a different approach: I set the focus to the last control in the dialog, which is always the OK button. Next, I want to emulate programmatically that a TAB is pressed and received by the dialog. So I wrote this (inside Dialog::onInit):

    m_buttonOK->SetFocus();
    wxKeyEvent key;
    key.SetEventObject(this);
    key.SetEventType(wxEVT_CHAR);
    key.m_keyCode=WXK_TAB;
    ProcessWindowEvent(key);

Now the focus indeed moves away from the OK button, but it does not wrap around to the first control. Only when I manually press TAB after the dialog opened, the first item gets focus.

Question: why does this wrapping around to set focus on first widget not work with the code shown above?

Aucun commentaire:

Enregistrer un commentaire