Convert to Uppercase?

Hi,
Is it possible to convert a TextField into upper case automatically.

My users will probably type in lower case - but when they click on the SAVE button, I would like all of the TextFields to be converted to uppercase before they are saved to the database.

I have looked in the Language Reference under TextField, but can not see anything relevant??

Can anyone please help me.

thank you all in advance.

Or do it in real time in the TextField TEXTCHANGE event

me.text=me.text.uppercase

If you use Dave’s suggestion, you will also need some sort of flag to keep from going into a TextChange loop.

Thanks guys - all OK now :slight_smile:

[quote=65751:@Dave S]Or do it in real time in the TextField TEXTCHANGE event

me.text=me.text.uppercase [/quote]

This prevents excessive loop :

if me.text<>me.text.uppercase then me.text = me.text.uppercase end if

Did you test that? I wouldn’t expect it to work, because string comparison in Xojo isn’t case sensitive. Use strcomp instead.

And setting a Boolean flag, even as a static, would be faster anyway.

Actually, scratch that. A static would be a terrible way to do it.

You are right, of course.

if strcomp(me.text,me.text.uppercase,0)<>0 then me.text = me.text.uppercase end if

Speed is not an issue here. Even the fastest typist will never be as fast as code.

I’m not sure what that means. If the field contains 5k of text, it’s faster to check a Boolean than to compare the string with each character typed.

You are right. I tend to look at text fields for short amounts of text. 5K looks much more like a Text Area.

I must be limited, I do not see how to use a flag to prevent repeat execution.

But on the other hand, TextChange is not really recurrent. So whenever it happens, me.Text = me.text.Uppercase would just work fine in most cases where the user enters lowercase. Strcomp would be of use to prevent execution when user enters uppercase.

Maybe a flag could be used from Keydown by detecting lowercase. But then if the user pastes lowercase, it would not work…

More like

static flag as boolean
if not flag then
   flag = true
   me.text = me.text.uppercase
   flag= false
end

Just don’t use static in subclass code because it’s shared across all instances and could produce unpredictable results. Make it a property instead. Static is probably fine in event code in the window.

[quote=65976:@Tim Hare]static flag as boolean
if not flag then
flag = true
me.text = me.text.uppercase
flag= false
end[/quote]

I do not see how your flag works. If not flag then flag = false ?

Exactly. I prefer to write it in a slightly different style:

if alreadyChanging then //do nothing Else alreadyChanging = true Me.text = me.text.Uppercase alreadyChanging = false End if

I just find that version easier to read. Works the same.

[quote=65986:@J Andrew Lipscomb]Exactly. I prefer to write it in a slightly different style:

if alreadyChanging then //do nothing Else alreadyChanging = true Me.text = me.text.Uppercase alreadyChanging = false End if

I just find that version easier to read. Works the same.[/quote]

Same difference. As it stands, code executes at every instance. The flag is entirely useless :wink:

Not at all. It prevents a stack overflow.

???

Please explain.

Because the textchange event is triggered by changing the text. Just try the code out without the alreadyChanging. It WILL make a nice crash.

First, I tested the code I posted above, and there is NO crash.

Then, WHERE do you set alreadyChanging besides in your short piece of code that makes sure alreadyChanging is always false ? As a matter of fact, this code in TextChanged executes ONLY when text is changed. So by hypothesis, only when text is changed. Your code has absolutely no effect.

There has apparently been a change in behavior since earlier versions of Xojo/Real Studio. In past versions, any change in the text would trigger the TextChange event, so changing the text within TextChange would cause TextChange to fire again which would cause another change in the text leading to another TextChange, etc.

It appears TextChange no longer does that, as Michel has pointed out and I’ve just tested, so he’s right, that flag code is no longer needed.