Modifying a Form Field on the onchange Event
One of the most common requirements that developers have is to examine a field value and then, if certain requirements are met, change its value before it gets written. Many such operations are possible with field rules, but sometimes an Xbasic script is more flexible.
In this case the developer wanted to change the Title field's value if it started with "The ". He wanted to remove "The " and append it to the end of the field. This was easily achieved by adding this code to the OnDepart event of the Title field.
First put the form in Design Mode by clicking the following.
Right click the field and select Events > onchange.
Paste the code and save the results.
dim name as C name = title.text if (left(name, 4) = "The ") then title.text = right(name, len(name) - 4) + ", The" end if title.refresh()
First, the name variable captures the value of the Title using it's text property.
Then it uses the LEFT() function to test if the first four letters are equal to "The ".
If the word is found, it uses the LEN() function to find the length of name.
It subtracts four characters and uses the resulting number to extract the trimmed version of the Title field using the RIGHT() function.
Next, it appends ", The" to the end of the trimmed field.
Then, it places the value back into title.text.
Finally, it refreshes the form display, so you can see the results.