wxTextCtrl: select all when control is clicked

I'm trying to have all text in a certain wxTextCtrl become selected when the user enters the control with the mouse. I've tried using an EVT_SET_FOCUS, and the handler looks like

self.OnTextSetFocus(self, event):
  event.GetEventObject().SetSelection(-1, -1)
  event.Skip()

...but that doesn't work, the text in the control remains unselected. How can I accomplish this (seemingly) simple task?

···

--
Roger Hyde
http://www.scriptspot.com/rhyde/

In Friday, May 28, 2004, 5:42:16 PM, Roger wrote:

I'm trying to have all text in a certain wxTextCtrl become
selected when the user enters the control with the mouse. I've
tried using an EVT_SET_FOCUS, and the handler looks like

self.OnTextSetFocus(self, event):
    event.GetEventObject().SetSelection(-1, -1)
    event.Skip()

...but that doesn't work, the text in the control remains
unselected. How can I accomplish this (seemingly) simple task?

    EVT_SET_FOCUS(self.textctrl, self.OnTextSetFocus)
    ...
    def OnTextSetFocus(self, evt):
        wxCallAfter(self.SelectAll)
    def SelectAll(self):
        self.textctrl.SetSelection(-1, -1)

-- tacao

E. A. Tacao wrote:

In Friday, May 28, 2004, 5:42:16 PM, Roger wrote:

> I'm trying to have all text in a certain wxTextCtrl become
> selected when the user enters the control with the mouse. I've
> tried using an EVT_SET_FOCUS, and the handler looks like

> self.OnTextSetFocus(self, event):
> event.GetEventObject().SetSelection(-1, -1)
> event.Skip()

> ...but that doesn't work, the text in the control remains
> unselected. How can I accomplish this (seemingly) simple task?

    EVT_SET_FOCUS(self.textctrl, self.OnTextSetFocus)
    ...
    def OnTextSetFocus(self, evt):
        wxCallAfter(self.SelectAll)
    def SelectAll(self):
        self.textctrl.SetSelection(-1, -1)

The reason for using wx.CallAfter here is that the text control does it's own selection management in its default EVT_SET_FOCUS handler. Using wx.CallAfter allows you to do your selection after the control has done its own.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!