Mask and text field behavior

I have a textfield mask to enter a phone number “##########” (limit text = 0) that gets formatted in the lost focus event.

If (for example), you need to change the last digit of the phone number, when you click on the end of the text field and backspace once, the entire field is erased.

There are no other event handlers for the field (except the lost focus event). Do you think it is the mask that is causing this behavior?

(Xojo Desktop 2013 r3.3)

Are you using a Mask, or Format ?

Please post the code in LostFocus. It will help see what is going on.

The lost focus event is not being triggered. I just put the cursor into the text field and hit backspace and the entire field is deleted.

No matter where I put the cursor in the field, once I hit backspace, the entire field is deleted.

I am using Mask to make sure only numbers are being entered, and then the number is formatted to a phone number in the lost focus event.

The LOSTFOCUS event only occurs when you leave that control (via TAB, or mouseclick)… a Backspace does not cause the LOSTFOCUS event to fire.

If pressing the BS key deletes all the text, then all the text was already selected (assuming you have no other code in the KEYDOWN or other events)…

Also “assuming” you are only supporting the phone # format of one locale, you can use the MASK for the whole thing (although personally, I avoid the mask property all together, and control things in the KEYDOWN event.

You might try putting this in the GOTFOCUS event


this will put the cursor at the end of the text, and make sure NOTHING is selected prior to the first key press by the user.

I quickly tried to reproduce the issue from David’s description[quote=286073:@David Schlam]There are no other event handlers for the field (except the lost focus event). Do you think it is the mask that is causing this behavior?[/quote]

I tried to quickly create a small project from your description in 2013R3.3 and none of what you tell happens. Could you try to make a small project that demonstrate the issue and post it. Otherwise we are shooting in the dark here.

Here is a small project that demonstrates this behavior.

Download Test Project

It doesn’t answer your question but maybe as an alternative; I use this subclass and it works pretty well:

and how is one supposed to download your sample… it seems what ever site you used only works with Windows…
as it wanted to download a ZipExtractor EXE (which of course a) I couldn’t use and b) wouldn’t trust anyways)…

So I won’t be able to provide you any more insight I’m afraid.

The arrow down button on the top right.
For me (on Mac) it just downloads the project.

The error was to use your own routine in LostFocus, instead of the Format property, which is the proper built in way to format a TextField.

I commented out the content of LostFocus and replaced it by this in the Format field of the Inspector for Textfield1 :

(###) ###\\-####

BTW about the LR page for Mask at Page Not Found — Xojo documentation

the examples of Mask should escape the dash.

TextField1.Mask="(###) ###\-####" //US Phone number, with area code

Otherwise the dash does not show in the field.


I was under the impression that mask did the job of format and mask, most likely because that’s the way it was done in 4D (which is what I used for years prior). So I see that format, when you select the textfield, is removed and then reformats the text after you lose focus.

I wish I spotted that sooner. It would have saved me a lot of coding.

Thanks a bunch.