TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Using Lists to Limit Field Values
 
   
 Posted: 7 February 2003
 
   
 
 Applies to: All versions
 
   
 
Audience: Intermediate
 
       
   

Question: How do I prevent users from entering values that aren't in the lists of my combo boxes?

Answer: Add code to the field object's (not the list object's) canDepart event that compares the value in the field to the values in the list, as shown in the following code sample:

method canDepart(var eventInfo MoveEvent)
var
   siCounter,
   siIndexNo smallInt
   uiListObj uiObject
   astrNames Array[] String
endVar

   ; Step 1: Check for a blank value
   ;         Remove this to require a value.

   if self.isBlank() then
      return
   endIf

   ; Step 2: locate the list object

   self.enumObjectNames( astrNames )
   for siCounter from 1 to astrNames.size()
      uiListObj.attach( astrNames[ siCounter ] )
      if uiListObj.class = "List" then
         quitLoop
      endIf
   endFor


   ; Step 3: compare the field value
   ; to the list values.

   siIndexNo = -1
   for siCounter from 1 to uiListObj.List.Count
      if lower( self.Value ) = (lower( uiListObj.List.Value ) then
         siIndexNo = uiListObj.List.Selection
         quitLoop
      endIf
   endFor

   ; Step 4: Display an error if not match was
   ;         found.

   if siIndexNo = -1 then ;// invalid selection
      msgStop( "Invalid Option", Self.Value +
               " is not a valid choice.  Please " +
               "choose one from the list or Press " +
               "Esc to cancel." )
      eventInfo.setErrorCode( cannotDepart )
   endIf

endMethod

While this seems complicated, it's a very straightforward approach, one that doesn't require much design work (e.g. one that doesn't force you to consistently name your list objects.) The basic process is:

  1. See if the field is blank. If so, skip the rest of the code.

  2. Locate the combo box's list object froma list of object's that the field object contains.

  3. Once the list object has been located, compare the field value to each value in the list. If found, then set a control variable to indicate the Selection number of the matching value and quit the loop.

  4. If the siIndexNo variable is still set to the error condition (-1), it means that the field value didn't match any of the values in the list, so an error appears and the user is prevented from leaving the field.

    Also, note that we tell them how to cancel any changes they made. This is good programming practise and helps minimize frustration in the long run.

You can use similar code to let the user add new values to the list. For example, if you populate your list dynamically, you may want to let your users modify list sources based on tables. To do this, modify Step 4 accordingly.

Notes and Tips

  • Tip: To require the user to enter a value from the list, remove (or comment out) the first IF block.

  • For brevity, we didn't verify that we actually located a list object (as might happen if the field were converted to a different display type). Production applications should include something to this effect and we leave this an exercise for the reader.

  • This implementation is case-insensitive. To perform a case sensitive search, remove the references to the lower() function.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]