Prompt IN a textfield

Is there a way in a Web app to show a prompt IN the field like the behavior of the title when starting a new conversation here.

Essentially you gain focus of the field and there is a prompt displayed. When you start typing the prompt disappears and is replace by your characters. If you backspace to an empty field the prompt returns when the last character is erased.

This cannot be done effectively with the TextChanged event because it is too slow.

I suppose I could craft some custom JavaScript.

I do this in most of my web fields using the GotFocus and LostFocus events.

Here’s pseudocode for the GotFocus event which triggers when they enter the field and blanks out the prompt so they can start typing:

if me.text="[your field prompt here]" then
me.text=""
me.style=StandardTextFieldFilled
end if

For the LostFocus event, it’s something like this:

if trim(me.text)="" then
me.text="[your field prompt here]"
me.style=StandardTextFieldPrompt
end if

That’s the basic concept. In my apps, I usually make those routines into global methods called from all the GotFocus/LostFocus events so that I can make changes in one place and the code isn’t duplicated in every single field.

At one point, I also did something like that using the KeyPressed event, but I think the reason I backed off from that was it seemed excessive to analyze every key press, although it might be worthwhile if you want to catch the Enter/Return key or other special keys anyway.

2014r1 added the CueText property to WebTextField.

I was not using 2014r1 for this project and missed that new feature. That does the trick.

The code snippet posted by Seth looks useful too for one or two places I want to do something a bit more dynamic that is not just a constant string.

Thanks!

Looks like the new CueText property does everything I wanted anyway–it can be modified programmatically, too.

MyTextField.cueText=myDynamicallyCalculatedString

I’ll be switching to it for all of my future stuff!

is it possible to have CueText on desktop application???

Yes.

Yes, in a TextField but not in a TextArea.

oh yeah… what if i want to stimulate CueText in TextArea?

Tickle it?

I mean simulate… Hehe

Have sample text in there, and remove it as soon as the text area gets the focus?

I posted two different ways to do that at https://forum.xojo.com/10370-textarea-cuetext