In VFP the textfield control had an event called valid.
It fired just before the textfield lost focus and gave the ability to prevent the textfield from losing focus.
I can’t find anything similar in xojo?
I have a textfield that i want to have some code run to validate if the entry is acceptable. If not, i wish to prevent the focus from moving away from it.
I have put in keytraps for enter, return, tab, up arrow and down arrow which validate before allowing focus change and all works well.
But if the user clicks another field, it does not validate first (as expected).
I have placed the validation code in the lost focus event and if validation fails, it issues me.setfocus.
This works, but you can see the focus shift, then shift back again, especially with focus ring on.
Also the approach of using lost focus with me.setfocus in it will not work for me.
One of my other controls is a date picker. So, if the entry is invalid on textfield1 and you click the date picker button, the date picker displays, allows entry, then when finished, returns focus to textfield1.
Thanks, but the validation i am using is to check against a database for unique entry.
for example, log in ID
window to create a log in ID, the textfield checks to see if the ID entered already exists.
Do it the way websites do then with a green check or red X next to the field and a note that says “Username is taken/available” If it’s taken, disable the create account button.
Better experience than a field you mysteriously can’t get out of.
You would have to use either the LostFocus event or a Timer that checks to see if the field needs focus (I wouldn’t put your validation code in the timer because that would get called a lot and your database wouldn’t like that.)
LostFocus isn’t always the ONLY place you need to do this validation
If you enter data in a text field then press a button the text fields lost focus event wont fire because buttons don’t get focus on a press
Not really. I trap for all the keys that will let them out of the field, but if they click away, I mark the field as invalid and let them go. If they click in a field, they expect the focus to go there. They just can’t save their changes until they correct it. When they try to update, I send focus back to the field.
Since all of the Tim’s are checking in on this, I couldn’t be left out :). @Tim Hare’s method is exactly what I use for a number of login apps (web and desktop).
Here is a solution that will make the field absolutely impossible to exit :
Prepare a plain box window containing a textfield the same dimension as the one you want to validate (Window2). Make it the same dimension as well (for instance 80x22)
In the TextField GotFocus event :
Sub GotFocus()
Window2.TextField1.Text = me.text//Copy the content to the modal textfield
Window2.left = self.left+me.left
Window2.Top = me.top+self.top
Window2.ShowModalWithin(self)
End Sub
That way what the user sees when the original textfield gets focus is in fact a modal window with a new textfield that he cannot exit until you have validated it.
When you validate, copy the Text property into the original textfield and close self.
Thanks everyone.
I have decided to go with tim’s idea (Parnell) originally and re-iterated by the other 2 tim’s! Lol.
Thanks Michel for your solution and I will mark that as the answer as it did answer the forum post while the others provided a better way to do it in this instance.
I will be using michels idea in future I’m sure.