wxTextCtrl SetFocus after last char? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
chemdream
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Sep 24, 2022 9:07 pm

wxTextCtrl SetFocus after last char?

Post by chemdream »

I have a wxTextCtrl that is used to search. So you may want to type, search, then type more and search more.

After pressing enter from a wxTextCtrl I bind an event to SetFocus back onto it.

However, when the SetFocus event runs, all the text in the wxTextCtrl is selected. You can't do anything until you use the mouse and click on the wxControl.

Any ideas on how to make the focus to after the text so the person can just keep typing?

Code: Select all

searchInput = new wxTextCtrl(Panel, wxID_ANY, wxEmptyString, wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
searchInput->SetValue("");

searchButton = new wxButton(Panel, wxID_ANY, "Search");
searchButton->Bind(wxEVT_BUTTON, &MyFrame::OnSearch, this);
searchInput->Bind(wxEVT_TEXT_ENTER, [&](wxCommandEvent&) {
        wxCommandEvent evt(wxEVT_BUTTON, searchButton->GetId());
        evt.SetEventObject(this);
        wxPostEvent(searchButton, evt);
        searchInput->SetFocus();
     });
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTextCtrl SetFocus after last char?

Post by PB »

Did you try calling text control's SetSelection() and/or SetInsertionPointEnd() after giving it focus? In case of further issues, I would also try calling those with CallAfter(), focus is sometimes best to be set outside another event handler.
chemdream
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Sep 24, 2022 9:07 pm

Re: wxTextCtrl SetFocus after last char?

Post by chemdream »

That worked! CallAfter was required.
Post Reply