Select the whole text on focus

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Select the whole text on focus

Aliket
Hi Vadim,

I have a dialog with some text fields on it, and i want the text to be selected when the ctrl gain focus. So i implemented this little game (select on focus) like this:

void OnSetFocus(wxFocusEvent& event)
{
    wxWindow* focus = wxWindow::FindFocus();

    if (focus)
    {
        wxTextCtrl* txtCtrl = wxDynamicCast(focus, wxTextCtrl);

        if (txtCtrl)
        {
            CallAfter([=]()
            {
                if (txtCtrl->HasFocus())
                {
                    wxSafeYield( txtCtrl, true );
                    txtCtrl->SetInsertionPointEnd();
                    txtCtrl->SelectAll();
                }
            });
        }
    }

    event.Skip();
}

Is this the right way to go ?

Note that if i remove wxSafeYield() call, the text got selected till the cursor where i actually clicked inside the ctrl which may result in the text got partially selected !!

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Loading...