How to Use the MaskedEdit Control for Currency Format

Last reviewed: March 18, 1997
Article ID: Q126676
3.00 WINDOWS kbprg kbcode

The information in this article applies to:

- Professional Edition of Microsoft Visual Basic for Windows,

  version 3.0

SUMMARY

The maskededit control requires that you enter a number for each of the pound sign (#) character place holders and enter a space for each of the ampersand (&) place holders. The control tests each character you enter to make sure it matches the specified mask. This is inconvenient for currency format. For example, if the mask is #####.##, you'd have to enter 00012.25; and if the Mask is &&&&#.##, you'd have to enter four spaces and then 12.25 (as in " 12.25").

This article shows by example how to gain the flexibility of entering any number. The application uses the period (.) to define the decimal point, and right aligns the number to the decimal point. Then you can enter the pennies.

MORE INFORMATION

Step-by-Step Example

  1. Start a new project in Visual Basic (ALT, F, N). Form1 is created by default.

  2. Add two Masked Edit controls (MaskedEdit1 and MaskedEdit2) to Form1, and assign these properties:

       Default Name    Mask       Format      PromptChar
       -------------------------------------------------
       MaskedEdit1     #####.##   $####0.00   (Space)
       MaskedEdit2     #####.##   $####0.00   (Space)
    
    

  3. Add the following code to the GotFocus event of MaskedEdit1:

       Sub MaskedEdit1_GotFocus ()
          maskededit1.SelStart = 0
          maskededit1.SelLength = Len(maskededit1)
       End Sub
    
    

  4. Add the following code to the KeyPress event of MaskedEdit1:

       Sub MaskedEdit1_KeyPress (keyascii As Integer)
          Call Masked_Key_Press(MaskedEdit1, keyascii)
          If keyascii = 13 Then SendKeys "{tab}"
       End Sub
    
    

  5. Add the following code to the GotFocus event of MaskedEdit2:

       Sub MaskedEdit2_GotFocus ()
          maskededit2.SelStart = 0
          maskededit2.SelLength = Len(MaskedEdit2)
       End Sub
    
    

  6. Add the following code to the Keypress event of MaskedEdit2:

       Sub MaskedEdit2_KeyPress (keyascii As Integer)
          Call Masked_Key_Press(MaskedEdit2, keyascii)
          If keyascii = 13 Then SendKeys "{tab}"
       End Sub
    
    

  7. Add the following Sub procedure to the general declarations section of the form:

       Sub Masked_Key_Press (MskEdit As MaskEdBox, keyascii As Integer)
    
          ' Calculate the location of the decimal point in the mask:
          mask_cents_pos = InStr(MskEdit.Mask, ".")
          mask_dollars = mask_cents_pos - 1
    
          ' Check for period keypress:
          If keyascii = 46 And MskEdit.SelStart < 6 Then
             tlen = MskEdit.SelStart + 1       ' Store current location.
             MskEdit.SelStart = 0
             MskEdit.SelLength = tlen          ' Highlight up to the current
             tempo = MskEdit.SelText           ' position & save selected text.
             MskEdit.SelLength = mask_dollars
             MskEdit.SelText = ""              ' Clear to the left of decimal.
             MskEdit.SelStart = mask_cents_pos - tlen
             MskEdit.SelLength = tlen           ' Reposition caret
             MskEdit.SelText = tempo            ' and paste copied data.
             MskEdit.SelStart = mask_cents_pos  ' Position caret after cents.
          End If
       End Sub
    
    

  8. Press the F5 key to run the program. Enter numbers, and observe the result when you press the period (decimal point) key.

This code shows a single method for handling various numeric input into the masked edit box. You may want to use this as a starting point and add additional code for special cases.


Additional reference words: 3.00 Format Mask Maskededit accounting right
justify justification dollar line up
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsCus


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.