Text Field - Empty Contents on Click

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
CoreySchroeder
Posts: 26
Joined: Mon Sep 14, 2015 4:31 am

Text Field - Empty Contents on Click

Post by CoreySchroeder » Sun Oct 11, 2015 5:37 pm

Hey,

I've got a bunch of text fields that I populate with default text when the card opens.
When the user clicks on the text field to input their own data, I'd like that text field to clear out the default text, and be automatically focused so the user can start typing.

The only way I've been able to figure this out is to put a see-thru button above the text field, that when clicked, will disable the button, put empty into the field and focus on the field.
Like this:

Code: Select all

on mouseUp
   if the short name of the target is "btnTest" then
      set the disabled of the target to true
      put empty into field "fldTest"
      focus on field "fldTest"
   end if
end mouseUp
This just seems a bit verbose to do such a simple task - I'm curious if any of you know of a better way?

Also, I'd like to put the default text back into the field if the user doesn't input any information.
So, lets say the user clicks on that field - the field clears out the content and focuses, but the user decides to click on another field instead and doesn't actually end up typing anything into the field that was previously clicked on - how can I easily put the default text back into the field?

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Text Field - Empty Contents on Click

Post by SparkOut » Sun Oct 11, 2015 5:52 pm

The dictionary is your friend here. Put these into the field script.

Code: Select all

on openField
   put empty into me
end openField

on exitField
   put "the default" into me
end exitField
Note exitField is sent when the contents have not changed since an openField message was sent, which fortunately is timed from the end of the openField handler. That is, you can put empty into the field in the openField handler, it's at the end of the openField handler that the contents are checked to compare with the contents at exitField.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Text Field - Empty Contents on Click

Post by dunbarx » Sun Oct 11, 2015 6:09 pm

HI.

Lots of ways to do this. Sparkout's way empties the a bit field earlier than you indicated. But is clean and compact and deserves study and consideration.

Here is another idea. Make a field and a button. In the button script, which emulates what you might do in an openCard handler, and can be clicked as needed to test:

Code: Select all

on mouseUp
   put "XYZ" into fld 1 --your defaultText
   set the lockText of fld 1 to "true"
end mouseUp

and in the field script:
on mouseUp
   set the locktext of me to "false"
   put "" into me
   select before me
end mouseUp

on exitField
put "XYZ" into me
end exitField
Now the user has to "terminate" the fact that he did not enter any text, perhaps by clicking somewhere else that defocuses the field, in order to trigger "exitField". You might play with a "mouseLeave" handler to make that happen at once, but you will then need to test the initial contents and compare with the contents after the cursor has left. All doable. Should be fun.

Craig Newman

CoreySchroeder
Posts: 26
Joined: Mon Sep 14, 2015 4:31 am

Re: Text Field - Empty Contents on Click

Post by CoreySchroeder » Sun Oct 11, 2015 7:56 pm

Thanks all for the reply!

I'd really like to also keep the text the user input - so if they click on a field, input text, that text is stored..
So if for whatever reason they click on the field again by accident, it uses their previously input text as the default text.
Does that make sense?

I just don't want someone to spend time filling out a field, then accidentally click that field and it gets overwritten by the default text - making the user start all over again.

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Text Field - Empty Contents on Click

Post by SparkOut » Sun Oct 11, 2015 8:13 pm

You can have 2 custom properties, one to store the basic new text "default" for the field and one to store the user input default text
In the card script

Code: Select all

on openCard
   --reset the defaults
   put the cDefaultFieldText of field "myField" into field "myField"
   set the cDefaultText of field "myField" to empty
end openCard
In the field script

Code: Select all

on openField
   put the cDefaultText of me into me
   --will be empty if new card navigation, otherwise restore what the user input
   send "selectAfter" to me in 0 milliseconds
   --by default when opening a field the cursor position is set to the point at which you clicked
   --this gives a moment for this to be handled, then the cursor/insertion point is moved to the end of the text already there
end openField

on exitField
   put the cDefaultText of me into me
end exitField

on closeField
   --if the contents have been changed by user, save the change for the new default
   set the cDefaultText of me to me
end closeField

on selectAfter
   --move the insertion point to the end of the text
   select after me
end selectAfter

Post Reply