How to Right Justify Standard Numbers in a Masked Edit Field

Last reviewed: June 21, 1995
Article ID: Q97141
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

SUMMARY

The Masked edit control does not provide a method to right justify numbers. Ordinary methods and the Format$ function do not work because the Masked edit control uses the underscore character to represent blanks in the text property. For example, if 300 is entered in a Masked edit field with a mask of #####, the text property would contain "__300" instead of " 300."

However, you can use the technique described in this article to right justify a Masked edit field using a standard number mask and format. This is done in three steps:

  1. Create a string of underscore characters that matches the length of the mask in the Masked edit control.

  2. Concatenate the text entered in the Masked edit control to the end of' the underscore string. This result is a string longer than the mask of the Masked edit control.

  3. Use the Right$ function to remove the extra underscore characters from the beginning of the string.

MORE INFORMATION

The following example demonstrates this process:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add the MSMASKED.VBX control to the project.

  3. Create the following controls on Form1, and assign the indicated properties:

       Default Name   Caption            Mask   Format
       ------------------------------------------------
       MaskedEdit1    (Not applicable)   ####   ####
       Command1       Right Justify
    
    

  4. Add the following code to the Command1_Click event:

       ' Ensure that the string is not already right-justified.
       If InStr((Len(MaskedEdit1.Text)), MaskedEdit1.Text, "_") =
          Len(MaskedEdit1.Text) Then
    
          ' The first String$ function creates the underscore string. The
          ' Format$ trims the text property of the MaskedEdit control.
          ' Enter the following two lines as one, single line:
          MaskedEdit1.text = Right$(String$(Len(MaskedEdit1.Text), "_") &
             Format$(Val(MaskedEdit1.Text)), Len(MaskedEdit1.Text))
    
       End If
    
    

  5. Press the F5 key to run the program.

  6. Enter two numbers into the Masked edit field, and click the Right Justify button. Notice that the numbers are right-justified in the field.


Additional reference words: 2.00 3.00 alignment align right-align
KBCategory: kbprg
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: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.