TextField SelectAll Problem ?

Matthew - Sorry for the late response - I got distracted with other things.

I downloaded your new code and it works great - It’ll take me a bit more time to understand the processing.

Now, to use your SATextField object to replace the current TextField Objects in my form:

I copy your SATextField object to my project
I delete the current GotFocus Event handlers from each existing TextField
I then change each existing TextField to be a SATextField

Correct ?

Thanks in advance - I really appreciate you hanging with me on this problem.

Do you know if Xojo plans a future fix for this problem ?

Ron Bower

That is correct. After changing the supers of your TextField’s on your Window to SATextField, no code is necessary for the auto SelectAll to occur on GotFocus (really SelChange) or by using the Tab key. I have been beta testing the FC 4.0 candidate, and it has yet to be fixed. You may wish to use the Xojo Feedback System and add your “two cents” about the issue. The more ‘hits’ a bug report gets, the more priority it becomes to be fixed. Unlike this one, which has been reported, many bugs like this don’t get reported because a developer will simply find a “work-around” (I’m guilty of it all the time…try using ISSecure in the HTMLViewer control SecurityChanged eventhandler using WebKit, simply will not fire the event ;-)) You’ll notice in the SATextField subclass that I actually had found two “work-arounds.” The first was moreso complex and involved looping through all controls on the parent window, testing if they were an SATextField, and if so, but was not the currently selected one, to DeselectAll, reset the flag, and SelectAll on the the current control. But, I commented it out (still there), and sought-out a “fewer lines of code” more efficient (optimized) method.

I have an even better control for you to take a lookat that makes the TextField control truly cross-platform. On Windows OS, TextFields do not retain their backcolor when disabled unlike Mac OSX and Linux. This TextField replacement handles that, as well as makes SelectAllOnFocus a Boolean, so that it can be used as a complete replacement for TextField without SelectAll always being enabled. Take a look. :slight_smile:

http://www./demos/txtBox.xojo_binary_project

Matthew - I’ve found one problem with the SATextField.

If you click into a blank text field and start typing, the first character you type is highlighted and the next character you type erases the first character.

Ron Bower

Apologies in haste I didn’t upload the working version…rather one of the broken copies. To prevent this, I opened the working copy, deleted the rest and resaved the working version.

http://www./demos/SelectAll.xojo_binary_project

Has been a hectic week…taking just a few extra seconds can make all the difference :slight_smile:

This copy also handles cursor insertion after selectall has occurred and won’t delete your previous text…

Looks very good - Thank you for all your effort.

I’m certainly dragging up an old post but I’m having the same issue using 2016R3 WIN7.

I guess the issue was never resolved. Disappointing when it should be oh so simple. Nevertheless, I’ll just have to live with it.

Post your code.

Hi Michel, I used the code in the TextField MouseDown event as posted by Tim above:

if self.focus <> me then me.SelectAll return true end

I had the same problem as Ron (the OP) in that I was seemingly unable to select the text field with the mouse at all.

After doing some more research, I came across this similar thread:
https://forum.xojo.com/25958-textfield-selectall-method/0

It looks like it’s a timing issue related to Windows & Xojo. As someone already pointed out, this issue hasn’t been a problem with VB or FileMaker (In my case) so it must be related to Xojo.

Anyway, from that thread above, In the MouseDown event I tried:

me.SetFocus me.SelectAll Return True

At least that code works in highlighting/selecting the entire text but then you cannot insert the cursor where needed if required.

Then I saw your code:

[quote]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.
[/quote]

Looks very promising but I cannot get it to work.

The first part of the code is a function? - where does this go? I tried putting it in the MouseDown event but got a NilObjectException error. I then created the Function MouseDown but not sure where to go from there.

Nowhere. The first and last line of each snippet is supplied by Xojo when you copy a function. It is the contents of the inspector fields for that event. The actual code is the stuff in between.

Thanks Tim. That’s what I initially thought. The NOE threw me off, thinking it was something I did - or didn’t do.

I’m used to seeing Sub/End Sub when printing out code, but not Function/End Function. Oddly, MouseDown is Function whereas MouseUp is Sub.

So, back to the code.

MouseDown:

If Self.focus.Handle <> Me.handle Then Me.SetFocus Return True End If

MouseUp:

Me.SelectAll

This causes a NilObjectException causing my app to shut down back to the IDE. It appears to happen on MouseDown. The text field in question is txtFuelWeight.

That would indicate that no control has focus - ie., self.focus is nil. Throw a check for if self.focus <> nil around the code in MouseDown.

Ah Yes!! Thanks Tim, that makes sense - code now works as expected :slight_smile:

On my application start-up nothing has the focus (I’ll need to look into that). In my haste to test, I was starting the app then immediately clicking into the text field causing the NOE.

If I select a different field first, then the error does not occur and the if self.focus <> nil code block is not required, but I see no issue leaving it there for ‘insurance’. All that happens now is if I start the app and select that field first, it does not highlight (no big deal) until I select another field first - but at least it doesn’t crash. This can be fixed by ensuring something has the focus on startup.

So in the end, this is the code that worked:

MouseDown:

[code] If Self.focus <> Nil Then

If Self.focus.handle <> Me.handle Then
  Me.SetFocus
  Return True
End If

End If[/code]

MouseUp:

Me.SelectAll

I’ve certainly learned some important things today. I’ll be using this method always - it’s not complex and it works perfect!

So thanks a lot Tim for your solution and a big special thanks to Michel for providing the original code.

Cheers :slight_smile:

ps. Someone should mark this thread as Solved.

Can I make a suggestion to simplify this greatly?

Make a TextField subclass. Override GotFocus:

Xojo.Core.Timer.CallLater(1, AddressOf FocusSelect) RaiseEvent GotFocus

Add the replacement GotFocus event.
Add the FocusSelect method:

Sub FocusSelect () Self.SelectAll() End Sub

That’s it. The same events will happen as before, which is something like GotFocus, MouseDown, MouseUp. But this time, SelectAll will happen on the next event loop, which is AFTER the mouse actions finish. This way, you don’t have to muck about with trapping the mouse actions, letting the system do its thing as expected.

Thom’s suggestion is solid. But to complete the thought we started, you can rewrite the code as

If Self.focus = Nil Or Self.focus.handle <> Me.handle Then
    Me.SetFocus
    Return True
End If

to get a more consistent response.

Well Tim, your above code is even better!! Now upon starting the app, when clicked, the text field is highlighted regardless if there is focus or not.

Certainly more succinct. I’ll use that in future but I have left the preceding code remarked out above it. Most of my coding is somewhat over-verbose for the simple reason that it may be some time before I go back and look at the code and therefore can be hard to make sense of it. I’m 15 months into this hobby project.

I’m really happy that this works. Often these “small” things sidetrack me. This morning I was "pulling my hair out with frustration", but now I’m like “a little girl dancing and skipping through the fields with flowers in his hair”.

What is handle anyway? To me it’s something that’s used to carry a larger item - or what your call-sign is on CB radio.

Thanks Thom for that suggestion, but it’s unfortunately beyond my comprehension - I know of subclassing but have never used it and I don’t really understand it yet - something for the future.

Am I the only one who thinks this is a terrible idea? If you click in a text field you should NOT select all the text. Standard behaviour is:

One click: set insertion point
Double-click: select line aka all text in a single-line text field
Triple click: select paragraph

Markus, there is merit to what you are saying.

Your “standard behaviour” in this instance, is not the same as mine.

For my purposes, the text field is a single line text field, and not a text area. It contains a numeric value which can be overstruck. It’s surprising, but I’ve found that even a simple double-click of the mouse can become irritating. I’ve used this method for many years with other programming languages. It was only today that I can confidently do this using Xojo.

I certainly would not employ this solution for a text area that contains paragraphs of text - unless there was a really good reason to do so. Overstriking a simple numeric default does make sense when you understand the reason why it may be required.

btw. It’s not a matter of wrong or right, but more about what solution best fits a scenario. Also important to point out that most of the programs I’ve ever written are analytical and therefore require a lot of numeric input.

Also having that option, then “I” can decide if it’s nesser or not. Best of both worlds. This solution is very worthwhile knowing for those programming in Windows.

Why not selectall in GotFocus instead? That way
It doesn’t matter how you get to the field - mouse click, tab, etc.

[quote=337532:@Tim Hare]Why not selectall in GotFocus instead? That way
It doesn’t matter how you get to the field - mouse click, tab, etc.[/quote]
Original post:

I have a form with several Text Fields. All are set to UseFocusRing.

I have the following in each textfield’s GotFocus event: Me.SelectAll

When I tab from field to field any existing text is highlighted whenever the textfield gets focus. However, if I just click on a textfield to give it the focus, any existing text is not highlighted.

I don’t recall having this problem with Real Studio.