KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Modifying form object values in real time using 4D commands and form events
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: September 30, 2005

Here is an exercise that uses commands to find and set the values of form objects, including edited text modification and real-time object updating, using form events On After Keystroke and On Data Change.

A combo box can be useful if an interface requires enterable (and tabbable for Mac OSX) areas but also needs dropdown objects for assisted entry. If the designer needs to restrict what values can be entered into a text area (in this case the combo box), they'll have to write code in the background to prevent unacceptable values from being entered. This example demonstrates a way to check the validity of user text entry in combo boxes where valid values are stored in text arrays of the same variable name, and it also demonstrates a way to provide the user with acceptable values based on their entered text in real time.

Some of the tools used in this exercise:
- ASCII(Keystroke) - captures user-input from the keyboard and converts it to the ASCII numeric value.
- Form Event: On After Keystroke - captures the point after which the user entered a keystroke and it is stored in the enterable object, or acted upon as a shortcut.
- Get Edited Text - captures text entered into the object in focus at the time of its execution.
- Highlight Text - highlights the text specified, from the start character to the end character in real time
- Self->{0} - the value which is displayed to the screen (for a text array form object) in this case. When this value is modified, it changes the displayed value in real time.
- Find in Array and the wildcard symbol to do a query of type "begins with"

With a few modifications you can make this code work with text objects, instead of text array objects. It's a matter of specifying the text array to be used, separately from the text form object pointer.

This example does not handle cases of "On After Edit". There is another tech tip that demonstrates this form event.

`````````````````````````````````````````````````````````````````````````````````````````````````````````

  ` Project Method: ComboBoxHelper

  ` Description: Helps the user find a value in a dropdown object (combo box) and prevents non-existent values from being entered

  ` Notes: This method should be called from the Object Method to properly retrieve form event, edited text, and pointer, unless you've retrieved these values by other means

  ` Notes: Generally the command would look like this: ComboBoxHelper(Self;Form Event;Get Edited Text)

  ` Notes: Don't forget to set form events for the objects!

  ` Parameter: $1 - pointer to form object of type text array

  ` Parameter: $2 - Form Event (numeric value)

  ` Parameter: $3 - "edited text" value from the form object - you can pass the command "Get Edited Text" as the third parameter from the object method

C_LONGINT($2;$formEvent;$length;$ArrayLocation;$key)

C_POINTER($1)

C_TEXT($3;$editedText)

$formObjPtr:=$1

$formEvent:=$2

$editedText:=$3


Case of

: ($formEvent=On After Keystroke )  ` processes text after it's been entered into the object

  $key:=Ascii(Keystroke)

   ` do not process certain keys

  If ($key#Backspace ) & ($key#Left Arrow Key ) & ($key#Right Arrow Key ) & (Not(Shift down))

    ` how many characters are in the edited text (for positioning the highlight area)

   $length:=Length($editedText)

   If ($length#0)

     `find the closest match to the entered text

   $arrayLocation:=Find in array($formObjPtr->;$editedText+"@")   

   If ($arrayLocation#-1)

     ` select the closest matching element in the combo box

   $formObjPtr->{0}:=$formObjPtr->{$arrayLocation}

     ` highlight text that has been added to the "edited text"

   HIGHLIGHT TEXT($formObjPtr->;$length+1;Length($formObjPtr->{0})+1)

  Else

   $formObjPtr->{0}:=$editedText  ` display only the edited text since there is no close match

   $formObjPtr->:=0  ` no elements selected

     ` set cursor location to end (no highlight)

   HIGHLIGHT TEXT($formObjPtr->;Length($formObjPtr->{0})+1;Length($formObjPtr->{0})+1)

  End if

 End if

End if



: ($formEvent=On Data Change )

   ` find the closest match in the array and select it

 $arrayLocation:=Find in array($formObjPtr->;$editedText+"@")

 If ($arrayLocation#-1)

   $formObjPtr->{0}:=$formObjPtr->{$arrayLocation}  ` select the matching element in the combo box

   $formObjPtr->:=$arrayLocation

 Else

   $formObjPtr->{0}:="none"  ` clear out the combo box text display

   $formObjPtr->:=0  ` no elements selected

 End if

End case

`````````````````````````````````````````````````````````````````````````````````````````````````````````

Commented by Bette Piacente on December 10, 2009 at 2:00 PM

ÊÊ` ----------------------------------------------------
ÊÊ` User name (OS): 4D Inc. - New Version for v11
ÊÊ` Date and time: 12/10/09, 13:54:20
ÊÊ` ----------------------------------------------------
ÊÊ` Method: ComboBoxHelper
ÊÊ` Description
ÊÊ` added the fourth param that allows you to wait until a certain number of chars are entered before executing the code
ÊÊ`
ÊÊ` Parameters
ÊÊ` ----------------------------------------------------

ÊÊ`Here is an exercise that uses commands to find and set the values of form object-time object updating, using form events On After Keystroke and On Data Change.
ÊÊ`
ÊÊ`A combo box can be useful if an interface requires enterable(and tabbable for Mac OSX)areas but also needs dropdown objects for assisted entry. If the designer needs (in this case the combo box), they'll have to write code in the background to prevent unacceptable values from being entered. This example demonstrates a way to check the validity of user text entry in combo boxes where valid values are stored in text arrays of the same variable na
ÊÊ`
ÊÊ`Some of the tools used in this exercise:
ÊÊ`-ASCII(Keystroke)-captures user-input from the keyboard and converts it to the ASCII numeric value.
ÊÊ`-Form Event: On After Keystroke-captures the point after which the user entered a keystroke and it is stored in
ÊÊ`-Get edited text-captures text entered into the object in focus at the time of its execution.
ÊÊ`-HIGHLIGHT TEXT-highlights the text specified, from the start character to the end character in
ÊÊ`-Self->{0}-the value which is displayed to the screen(for a text array form object)in this case. When this value is modified, it changes the displayed value in rea
ÊÊ`-Find in Array and the wildcard symbol to do a query of type"begins with"
ÊÊ`
ÊÊ`With a few modifications you can make this code work with text objects, instead of text array objects. It's a matter of specifying the text array to be used, separately from the text form object pointer.
ÊÊ`
ÊÊ`This example does not handle cases of"On After Edit". There is another tech tip that demonstrates this form event.

ÊÊ`````````````````````````````````````````````````````````````````````````````````````````````````````````

ÊÊ` Project Method: ComboBoxHelper

ÊÊ` Description: Helps the user find a value in a dropdown object (combo box) and prevents non-existent values from being entered

ÊÊ` Notes: This method should be called from the Object Method to properly retrieve form event, edited text, and pointer, unless you've retrieved these values by other means

ÊÊ` Notes: Generally the command would look like this: ComboBoxHelper(Self;Form Event;Get Edited Text;3)`starting at position 3

ÊÊ` Notes: Don't forget to set form events for the objects!

ÊÊ` Parameter: $1 - pointer to form object of type text array

ÊÊ` Parameter: $2 - Form Event (numeric value)

ÊÊ` Parameter: $3 - "edited text" value from the form object - you can pass the command "Get Edited Text" as the third parameter from the object method

ÊÊ` Parameter: $4 - the numeric position of the first letter of the "edited text" value from the form object {optional}


C_LONGINT($2;$formEvent;$length;$ArrayLocation;$key;$4;$startingAt)

C_POINTER($1)

C_TEXT($3;$editedText)

$formObjPtr:=$1

$formEvent:=$2

$editedText:=$3

If (Count parameters=4)ÊÊ` Modified by: Randy Shepherd (12/10/09)

$startingAt:=$4
Else
$startingAt:=1
End if



Case of

: ($formEvent=On After Keystroke )ÊÊ` processes text after it's been entered into the object

ÊÊ`$key:=Character code(Keystroke)ÊÊ`v11
$key:=Ascii(Keystroke)ÊÊ`v2004


ÊÊ` do not process certain keys

If ($key#Backspace )Ê&Ê($key#Left Arrow Key )Ê&Ê($key#Right Arrow Key )Ê&Ê(Not(Shift down))

ÊÊ` how many characters are in the edited text (for positioning the highlight area)

$length:=Length($editedText)

If ($length#0)Ê&Ê($length>=$startingAt)

ÊÊ`find the closest match to the entered text

$arrayLocation:=Find in array($formObjPtr->;$editedText+"@")

If ($arrayLocation#-1)

ÊÊ` select the closest matching element in the combo box

$formObjPtr->{0}:=$formObjPtr->{$arrayLocation}

ÊÊ` highlight text that has been added to the "edited text"

HIGHLIGHT TEXT($formObjPtr->;$length+1;Length($formObjPtr->{0})+1)

Else

$formObjPtr->{0}:=$editedTextÊÊ` display only the edited text since there is no close match

$formObjPtr->:=0ÊÊ` no elements selected

ÊÊ` set cursor location to end (no highlight)

HIGHLIGHT TEXT($formObjPtr->;Length($formObjPtr->{0})+1;Length($formObjPtr->{0})+1)

End if

End if

End if



: ($formEvent=On Data Change )

ÊÊ` find the closest match in the array and select it

$arrayLocation:=Find in array($formObjPtr->;$editedText+"@")

If ($arrayLocation#-1)

$formObjPtr->{0}:=$formObjPtr->{$arrayLocation}ÊÊ` select the matching element in the combo box

$formObjPtr->:=$arrayLocation

Else

$formObjPtr->{0}:="none"ÊÊ` clear out the combo box text display

$formObjPtr->:=0ÊÊ` no elements selected

End if

End case

ÊÊ`````````````````````````````````````````````````````````````````````````````````````````````````````````