KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Display a countdown of character space left in a text variable during user entry
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: December 13, 2005

In some cases the designer needs to limit their text variables to a maximum size. For example, the designer wants to allow the user to enter a company description for their business partner records, but it cannot exceed 255 characters. The designer could easily truncate the variable when saving the record, but it's not very user friendly.

Instead, count down the number of characters that the user can enter, and display it on the form so that they know when they've reached the limit. You can also prevent the user from entering more than the maximum number of characters as well.

Here is what is needed:
1) On the user entry form: The user entry variable such as the description variable or any text variable of restricted length
2) On the user entry form: Numeric variable with name "Count" (under the description variable)
3) On the user entry form: Text object with string "characters left" (directly to the right of the count variable)
4) In Form Method's "On Load" section:

  ` Form Method

Case of

: (Form event=On Load )

  `......

C_TEXT(EntryVariable)

EntryVariable:="Initial value from record or elsewhere"

C_LONGINT(myCount;MaxEntry)

MaxEntry:=255

myCount:=MaxEntry-Length(EntryVariable)  ` this user entry object must be defined

  `.......

End case


5) Here is the code to use in your EntryVariable's object method:

  ` Object Method: EntryVariable

  ` Description: Check the size of the text entered and limit its size to 255.

  ` Update the Count variable to show how many characters are left.

C_LONGINT($formEvent;$length)

$formEvent:=Form event

Case of

: ($formEvent=On After Keystroke ) | ($formEvent=On After Edit )

$length:=Length(Get edited text)

If ($length>MaxEntry)

$length:=MaxEntry

  ` remove excess characters during entry

Self->:=Substring(Get edited text;1;MaxEntry)

End if

myCount:=MaxEntry-$length

End case


Example: