How To Force Input To ALL CAPS
Previous Topic  Next Topic 

Problem:        How To Force Input To ALL CAPS


Solution:        On occasion you may wish to force input in an edit control to ALL CAPS.  There are several different ways to accomplish this, for example using the AfterChange event that gets fired when a control changes its value, or the OnValidate event which fires before the form's current data is saved to the linked table.  This article presents a method for filtering the characters as they are inputted via the OnKey event, converting them to ALL CAPS before they get posted into the edit control.


This can be accomplished using a global function in conjunction with the PasteChars function of the new SysUtils extension (introduced with  Satellite Forms 7.1).  Let's start with an IsUppercaseInput global function that just looks at the input and decides if it is uppercase text or not.  For simplicity, this function treats "a".."z" as Not Uppercase, and everything else as Uppercase.  You could of course extend these routines to handle the international accented chars too -- the example below is for just for unaccented "a".."z":


Function IsUppercaseInput(AKey)

'use AKey values directly not characters here because

'in SF script "a" = "A" when doing string comparison

if ((AKey >= 97) and (AKey <= 122)) then

  IsUppercaseInput = false

else

  IsUppercaseInput = true

endif

End Function



Now, in the form's OnKey event we can check if the input was uppercase and if not we can post the uppercase version of that key into the keyboard input queue (using PasteChars from the SysUtils extension), and discard the original lower case input.  In this example, we are performing the IsUppercaseInput filtering on an edit control name edAllCaps only, and leaving input to other controls unfiltered:


dim AKey, VKey, MKey

GetLastKey(AKey, VKey, MKey)


'if input to edAllCaps control then check for uppercase input only

if Forms().GetFocus = edAllCaps.Index then

if not IsUppercaseInput(AKey) then

  'input is from lowercase a..z so post uppercase key instead

  'uppercase letters ASCII values are 32 less than lowercase values

  SU_PasteChars(CHR(AKey - 32))    'post uppercase key to input

  Fail 'cancel the lowercase key

endif

endif



That should do it.  You can expand it to other controls by adding additional IsUppercaseInput tests in the OnKey script, and you can expand the comparison to accented chars by modifying the IsUppercaseInput global function.


Keywords:        IsUppercaseInput, upper case, OnKey, capitals, GetLastKey


KB ID: 10061 

Updated: 2007-07-11


Satellite Forms KnowledgeBase Online

Satellite Forms Website Home