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!

no overwrite on last character of textbox with a max length

Status
Not open for further replies.

longda

Programmer
Jul 24, 2001
66
US
I have some textboxes on a form that the user must enter data into. I have a maxlength on all of them, and I have set confirm on so the user must tab out of the textbox. This will however just place the cursor of the textbox to the last character and if the user continues to type then that last character is continuely overwritten. My question is how do I stop anymore characters being entered when the user hits the maxlength.

Example:
The maxlength of textbox txtLastName is 5
The user enters "Smith"
This would be fine, however if the user tried to enter "Smiths" the textbox looks like this "Smits" because it overwrites the last character. How can I stop this with out creating alot of extra code.

Dave Long
 
You can't. VFP does not allow the cursor to be placed after the last character. You can SET CONFIRM OFF which will allow the focus to move out of the field once it has been filled, but your extra characters will simply spill over into the next field.

Ian
 
Ian thanks for letting me know. I went ahead and did it the hard way and wrote some code in keypress and interactive change events for my textboxes. Thanks for the help.

Dave Long
 
Out of curiosity, what did you put in for your event code? I would think the simplest solution would be something like this:
[tt]
PROCEDURE keypress
lparameters nKeyCode,nShiftAltCtrl
IF INLIST(nKeyCode,1,4,5,6,7,9,13,19,24,27,127)
return
ENDIF
IF LEN(ALLTRIM(THIS.VALUE))=THIS.MaxLength
NODEFAULT
ENDIF
[/tt]
The INLIST command checks to see if the keypress was a cursor movement key. I might have missed one, but I think I got them all. 7 and 127 are DEL and BACKSPACE, so they should be allowed as well.

After this, the length of the contents are compared to the maximum allowed length of the field. If it is full, it doesn't allow new keypresses to be added.

Is that similar to how yours was done?

Ian
 
Boy Dave I'm gone 2 days and you're lost already?!
Hang in there bud...You are the man!
 
Ian

Sorry it took this long to reply but i was out of town this weekend and wasn't able to post the code. I liked what you did in your example. I have never used the inlist command or even heard of it until your post. I've only been programming in vfp for 8 months and alot of what I've learned has come from here so I appreciate the help.

My textbox is named txtLastName, but is not bound to a field because it later gets stored into a variable depending on what the user does. My code is as follows

procedure keypress
LPARAMETERS nKeyCode, nShiftAltCtrl
public strTextboxString
if (nKeyCode >= 65 and nKeyCode <= 90) or (nKeyCode >= 97 and nKeyCode <= 122) or nKeyCode == 45
if this.sellength = 0
strTextboxString = this.value
else
strTextboxString = &quot;&quot;
endif
else
strTextboxString = &quot;&quot;
endif
return

procedure interactivechange
if len(alltrim(strTextboxString)) = 20
?chr(7)
this.value = strTextboxString
this.selstart = 19
endif
release strTextboxString
return

What this does is if the user has reached the maxlength a bell will sound and replace the value with what they already typed in to eliminate the the extra character. If however they highlight some characters to overwrite them it will allow it. It also only accepts alpha characters and hyphens (in case of a multiple last name ex: &quot;Smith-Jones&quot; if wife kept her maiden name). This also forces the user to tab out of the textbox because of &quot;Set Confirm On&quot; in my start up program. Thanks for you help.

Dave Long
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top