Dynamic Masking in Visual DataFlex

by Ivan Larrondo V.

Sometimes having a dynamic mask on a data entry object can be very usefull. For example, multiple currencies in a general ledger module, or the layout of zipcodes and phonemumbers. This is by default not possible in Visual DataFlex and not supported by DataAccess. You can dynamically change a mask only before the object is displayed on the screen. This means that you will normally have to close an application before you can change a mask. Not really a satisfying answer.

Then again, it is DataFlex, so there's always an answer, the trick is usually in finding the correct answer to it.
So we need to change the mask before it is displayed. This actually can be read as: if the object is already displayed with a mask, we can take it off the screen, change the mask and then display the object again. You can do this with the page_object message. A Send Page_Object False, will remove the image from the screen and a page_object True will display it again.

So if that works, we only need a good hook for it that will change whenever a new record is found. A really good place for this is the refresh message, because this is send to every data-aware object as soon as a new record is found in the view's server object.
The refresh message is sent quite often, so that means that we will want to keep the code slim. Below is a snippet from our example code.

Procedure refresh integer mode
  Local String sState
  Local Integer hoServer
  Forward Send refresh mode
  Move (Server(Self)) To hoServer
  If (hoServer <> 0) Begin
    If not (current_record(hoServer)) Procedure_return
    Get File_field_current_value Of hoServer ;

                      File_field CUSTOMER.STATE To sState
    Move (trim(sState)) To sState

    Send Page_object False
    If (sState='CA') ;
      Set Form_mask To '#####-###'
    Else ;
      Set Form_mask To '##-###-###'
    Send Page_object True
  End
End_procedure // refresh

If you want to test this yourselves, you can download an example here that can be copied into the order-entry example. To test this, simply copy this file into the AppSrc folder and register the view in the IDE with Register External Component... menu option in the components menu. The code is in the Customer zip object and the masking of the zipcode will change if the state is California.