Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form input box max length

Status
Not open for further replies.

Canonian

IS-IT--Management
Aug 1, 2017
9
US
I have a simple input text box with an 85 character maxlength set, with the control source set to a field in the database that allows 100 characters. No other settings format, input format etc. When you are typing and reach the limit instead of simply stopping it jumps to the beginning of the field and allows you to keep typing over everything from the beginning of the input box. Is there a way to stop the typing cold when the 85 characters is reached?
 
it jumps to the beginning of the field

Do you mean the beginning of the next field? If so, check that you have SET CONFIRM ON.

If you mean the beginning of the same field ... well, I've never come acrosss that situation.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I've just tried to reproduce your problem. This is what I am seeing: When you reach the maximum length (as defined by MaxLength), the insertion points remains at a position immediately to the left of the last character. As you continue to type, you overwrite the last character. The insertion point never jumps to the beginning (of the same control).

Have you perhaps got some other code somewhere that might be causing this to happen?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Canonian,

I once saw what you described, but most of the time what Mike describes.
Usually you stick to the last position you can write to and overwrite when typing, while a bell sound is played because you reach the maxlength.
What you want is not possible anyway, as you can't put the text cursor after the maxlength position, you can only set it to SelStart 0 up to maxlength-1.

So what you want would only be possible by setting maxlength 1 higher than you actually want to allow, then use keypress or interactivechange or both in a way to get the behavior you want.

Chriss
 
This works for me:

1. In form init or load do
Code:
Set Confirm on
2. Set textbox.MaxLength to 86
3. In Textbox. Interactivechange event put:
Code:
If This.SelStart >= This.MaxLength-1
   This.Value = Left(This.Value,This.MaxLength-1)+" "
   This.SelStart = This.MaxLength-1
   This.SelLength = 0
Endif

Chriss
 
myself said:
2. Set textbox.MaxLength to 86
Well, in general one more than you need.

I also checked whether that fails, if the underlying field of the dbf/cursor only allows 85 (or in general maxlength-1) and it does not fail. Whether the maxlength is longer than the field controlsource allows or is unlimited, you can enter more text and it will be immediately truncated once you leave the textbox.

What matters for the functioning of the interactivechange is simply, that you can allow an additional space character which just allows the text cursor to be after the (maxlength-1)th text character and so you get the behavior you want to see about the text cursor and continuing to write does nothing. The user can leave with ENTER or right arrow or TAB, of course, and in Lostfocus I'd perhaps remove the additional space or don't even put it in there. Actually, it's optional, the text cursor can still be at SelStart = maxlength-1, if there is no space. You could just visually show you're at the maxlength when you also set SelLength=1, then the space would be shown inverted.

And, last not least, you can also "ring the bell" by doing
Code:
?? chr(7)

So maybe try
Code:
If This.SelStart >= This.MaxLength-1
   This.Value = Left(This.Value,This.MaxLength-1)+" "
   This.SelStart = This.MaxLength-1
   This.SelLength = 1
   ?? chr(7)
Endif
in conjunction with this in the valid event to remove this space:
Code:
If Len(This.Value) = This.Maxlength
   This.Value = Left(This.Value,This.MaxLength-1)
Endif

or don't add it by using this variant of ineractivechange:

Code:
If This.SelStart >= This.MaxLength-1
   This.Value = Left(This.Value,This.MaxLength-1)
   This.SelStart = This.MaxLength-1
   This.SelLength = 0
   ?? chr(7)
Endif

In both cases the maxlength still has to be set one higher than you want the actual limitation, the last position is needed to get the text cursor on that extra position and thereby not overwrite what was entered into the last position by continuing to type.

In many offices I know the workstations will have no sound, so no one disturbs others with noise, so a visual hint of getting to maxlength with the additional inverted space is a good choice, I think, you may remove the ?? chr(7), too.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top