TextField SelectAll Method

I’m having a hard time getting SelectAll to work when I click a text field.

I put Me.SelectAll into a GotFocus event handler, but the text is not selected.

What am i missing ?

Ron Bower

What I think,
if you click in the textfield you set the cursor. that means sellength = 0

I had that thought also, so put the following in the GotFocus event handler

Me.SelStart= 0
Me. selLength = Len( Me.Text )

Still not working

you can try

me.SelectAll Return True

in MouseDown

Nope, no joy !

in Text Field MouseDown event handler

me.SelectAll
Return True

I can click in the text field, but the cursor never stays there.

You must be in Windows. The code previously posted does not work there.

Here is what I did that works here :

Function MouseDown(X As Integer, Y As Integer) As Boolean if self.focus.Handle <> me.handle then me.SetFocus Return True end if End Function

Sub MouseUp(X As Integer, Y As Integer) me.SelectAll End Sub

In MouseDown, I test if the TextField already has focus. If not, I set focus, return true, and in MouseUp selectall. If not (the TextField already has focus), I let the user click wherever he pleases to put the caret within the text.

Bingo !!!

That’s the first solution that actually worked. Many thanks Michel.

Can anyone explain why this seemingly simple method does not work, as documented by Xojo, in Windows?

Ron Bower

[quote=214733:@Ron Bower]Bingo !!!

That’s the first solution that actually worked. Many thanks Michel.

Can anyone explain why this seemingly simple method does not work, as documented by Xojo, in Windows?

Ron Bower[/quote]

SelectAll does work perfectly fine. What does not work is using it in MouseDown. I believe what happens is that at the moment MouseDown happens, the control has not yet set SelStart.

You can see that very well with

Function MouseDown(X As Integer, Y As Integer) As Boolean system.DebugLog str(me.SelStart) End Function

Each time you click, the value of SelStart is the previous one (zero when the first click).

So what happens with the previous code is

  • SelStart does occur (I verified with SelLength)
  • The control sets SelStart and SelLength becomes zero

The reason why my code works is that in MouseUp, the caret has already been set, so SelectAll sticks.

Thanks for the explanation Michel. I still don’t understand why SelectAll doesn’t work in the GotFocus event.

I tried to explain.

It is a question of timing.

Do this in the GotFocus event :

Me.SelectAll Break

You will see that it did work ; all the text is selected.

Hit continue, and because the control actually sets the insertion point after that (after MouseDown as well), SelStart is set where the user clicked, and no longer is the text selected.

I guess on Mac the control sets the insertion point before MouseDown happens.

Okay, Michel, got it now. Thanks for the patience.

You’re welcome.

I think this is clearly a bug.
I’ve posted this case: <https://xojo.com/issue/45416>
But there are other cases saying more or less the same, and the bug persists after some years.

No, not a bug. It’s a platform difference that Xojo can’t insulate you from.

I’m sure you are right in your first assertion. Not so sure in the second one. All applications I use on Windows “select all” when you click a textfield, and I doubt the programmers had to use the trick every time. I used vb for many years and I can’t remember I had to do all this every time.

And that is the default behavior for Xojo on Windows. The entire text is selected on gotfocus. Unless you do something special with mousedown/gotfocus, and then the platforms behave differently. I assumed that the OP had some reason for needing special code in gotfocus. Otherwise, selectall happens by default.

Tim,
You are right. On Windows you must double-click a TextField for SelectAll.
If you want to select all with a single click you have to program yourself.
In the MouseDown event:

me.SetFocus me.SelectAll Return True
But if it is not explained at the Language Reference it is a bit difficult to know that Return True must be present.