Limit a text field to a single line?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Limit a text field to a single line?

Post by urbaud » Fri Mar 15, 2013 1:46 am

Is there a way to limit a text field to a single line so that the user could not use the return key to make a second line? I've tried to find info on this topic but have not been successful. One would think it would be a property of the field like in Visual Basic where there are single line text fields and "note" text fields, meaning they would allow for as many lines as the user wanted. Any help would be appreciated.
urbaud

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Limit a text field to a single line?

Post by Simon » Fri Mar 15, 2013 2:00 am

Hi urbaud,
I might be reading this wrong, but couldn't you trap the
on returnInField
&
on enterInFiled messages and not let them pass?
I'm sure there must be an easier way though.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Limit a text field to a single line?

Post by dunbarx » Fri Mar 15, 2013 3:52 am

I could not resist.

What Simon says.

In your field script:

on returnInField
end returnInField

There is no native one-liner, as you mentioned. Better to roll your own. A label field sort of does what you wished for, but not really.

Craig Newman

Sorry, Simon

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Limit a text field to a single line?

Post by Simon » Fri Mar 15, 2013 4:05 am

What Craig says...
but isn't the Enter key on a keyboard also used? err... wait ... looking down I see mine is labeled Enter, so maybe I mean Return.
I think I read somewhere that Win machines have it all wrong and that Enter is used for numeric keypads and Return should be the normal linefeed.
At any rate I think
on enterInField
end enterInField
Should be used as well.

Simon
EDIT: From the LC dictionary
Comments:
The Enter key is usually located on the numeric keypad. It is a different key from the Return key (confusingly labeled "Enter" on some keyboards), which is usually located above the right-hand Shift key.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Limit a text field to a single line?

Post by dunbarx » Fri Mar 15, 2013 4:12 am

As usual there are always little things kicking around.

You have your returnInField handler, right?

Make sure you set the "dontWrap" of the field, or you might see what appears to be multiple lines, even though there is only one. In fact, if you play around a little with a field with a lot of text and with its "dontWrap" set to "false", you can place the cursor lower down just as if you allowed multiple lines. It is the difference between physical and logical lines.

So you may want to think about limiting the amount of text allowed in a single line, so it physically just fits, unless the excess that runs over the edge is not a concern. Can you do that? Write back if not.

EDIT:

Simon, I don't think sending an "enter" to an editable field will do much, especially move the selection down a line.

Craig Newman

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Limit a text field to a single line?

Post by shaosean » Fri Mar 15, 2013 6:39 am

You can trap the messages as noted above, or you can set the autoTab property of the field to true

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Limit a text field to a single line?

Post by snm » Fri Mar 15, 2013 7:13 am

You can use textChanged message and check if there is cr, lf or crlf, and then replace with space or empty if yes.

Marek

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Limit a text field to a single line?

Post by jacque » Fri Mar 15, 2013 8:46 am

Any editable field with it's autotab set to true will tab to the next field when the text reaches the last line and the user hits the return key. If you size the field so it is only one line high then you have a limited one line field. No script required.

Oops, I see shaosean beat me to it.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Limit a text field to a single line?

Post by snm » Fri Mar 15, 2013 9:32 am

You are right Jacque and Shaosean, but without script enter moves you out of the field.

So when somebody hits "enter" accidentally, he must come back to the field in place when he finished. Using the script the "enter" is omitted or replaced by any other character (space, tab, dot, ... or empty=nothing).

It depends from what you need and there are two different solutions.

Marek.

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Limit a text field to a single line?

Post by urbaud » Wed Apr 10, 2013 12:49 am

First of all I want to thank all of you who made suggestions regarding this post. Secondly, I'm now really confused. I'm not sure what to do and which suggestion to use. Maybe if I'm more specific that will help you to know what I'm trying to do. Picture this: one large text field (t1) and one small text field (t2) and a button labeled "Find". The handler in the Find btn is:

Code: Select all

select before text of fld "t1"
put fld "t2" into searchText
find chars searchText in fld "t1"
Rather than use the mouse to click on the Find btn for each instance of “found text” I’d like to use the Enter or Return key. My preference is the Enter key. I enter the characters I want to find into fld “t2” and click the mouse on the Find button. It finds the first instance of the text to be found. Assuming there is more than one instance of the text to be found I’d like to press the Enter key each time. But, if I press Enter it moves the cursor in fld “t2” to the next line. If I use the AutoTab as suggested that doesn’t really help either. Some of you suggested that I use an “enterInField” handler in fld “t2”. Problem is, I don’t know what the code should be in the handler and will it stop the cursor from going to the next line? As you might have guessed I’m somewhat new to LC. So, any suggestions, etc. would be much appreciated.
urbaud

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Limit a text field to a single line?

Post by Simon » Wed Apr 10, 2013 1:03 am

just wrap your script in:
on returnInField
--Your script
end returnInField

on enterInField
returnInField
end enterInField

Make sure this script is in the field script

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Limit a text field to a single line?

Post by urbaud » Thu Apr 11, 2013 7:41 pm

Simon,

Thank you for your reply. I appreciate your comments and the suggested code. Thanks again.
urbaud

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Limit a text field to a single line?

Post by Simon » Thu Apr 11, 2013 9:58 pm

Hi urbaud,
What you are trying to do with the word search is very possible in LC. I think you should check the dictionary for these terms that may help you:
wordOffset (note the part on words to skip)
wholeMatches
textChanged

Have fun!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply