• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1302
  • Last Modified:

MaskedEdit box right justify

I use VB5 with SP3. At one stage all MaskedEdit box text was right justified. No mask was used. All 2 decimal place currency was correctly aligned with the points in a vertical line. Now, for some reason, all text is LEFT aligned. This is unacceptable for currency alignment. If I need an updated control, where do I find it?
0
PeterN
Asked:
PeterN
  • 5
  • 5
  • 2
1 Solution
 
MirkwoodCommented:
it's not possible to align the MaskEdit content to the right... but I have an alternate solution if you'Re willing to try it.

Use a text box and put this code in the Change() event...

it checks to see if the last character added was numeric or not.. and if it wasn't it removes it

Private Sub Text1_Change()
    If Not IsNumeric(Text1.Text) And Text1.Text <> "" Then
        Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
        Text1.SelStart = Len(Text1.Text)
    End If
End Sub  

0
 
MirkwoodCommented:
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:


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

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.

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

MORE INFORMATION
The following example demonstrates this process:


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

Add the MSMASKED.VBX control to the project.

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

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


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


Press the F5 key to run the program.

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.  

0
 
PeterNAuthor Commented:
Mirkwood, thanks for your prompt reply. However, I can assure you that at one stage ALL my MaskedEdit box texts were RIGHT justified! Even dates were right justified. I cannot remember what software I loaded which reset to left justify. It might have been VB4 to VB5, but I am really not sure.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MirkwoodCommented:
Nope, no version of the maskeditbox has EVER supported right alignment. So either you changed the masked or used another edit control.
0
 
PeterNAuthor Commented:
Sorry - I have to disagree. I spent ages in VB4 with Win 95 trying to right justify-with no success. I THINK I then moved to a new machine with Win 98 (still VB4) and the alignment of ALL MaskedEd's was RIGHT justified. I was delighted that Microsoft had solved the problem! I simply assumed MicroSoft had seen the light and had provided an updated control. I then upgraded to VB5 and the alignments were back to 'bad old left' again. So somewhere there was a different control which I must have overwritten. I think my sequence of events is correct but I can assure you that I was not imagining the right justification!
0
 
vettrangerCommented:
I won't get into the 'never right' controversy, but its clear that at least currently the Masked Edit box has no alignment property.

So you have four choices :
1. Create your own by making your own control
2. Buy a third party control, I'd be shocked if you can't find a package of controls with one that fits your bill
3. Use a workaround like those supplied above
4. Don't use the masked edit control. For currency, I use straight right aligned text boxes in control arrays, and I use Format in the change event to keep the user input formatted to my preference for that application.
0
 
PeterNAuthor Commented:
OK - I admit defeat. Vettranger's textbox solution will have to be my solution! I hope MicroSoft subscribe to Experts-Exchange. Maybe they'll learn something!
0
 
vettrangerCommented:
Here it is as an answer then. ;-) And since it turns out that way, I felt compelled to offer more detail ... hope it helps.

BTW, when you're changing data in the change event, especially with control arrays, take care not to get unexpected change event cascades.

Even in my suggestion, you may be better off to actually put the format statement in the lostfocus event rather than the change event. Otherwise you'll have to include a little extra code to make sure that you don't format in the middle of the user's typing.

BTW, bound text boxes in VB6 get a dataformat property that does exactly what you need. I didn't think about it earlier since you specified VB5, but decided to mention it.
0
 
MirkwoodCommented:
I reject to the fact that my answers were rejected and I wouldn't have rejected the points :)
0
 
PeterNAuthor Commented:
Mirkwood, please accept my apologies. I have re-read your answer and it also uses the TextBox as a solution. I don't know how to organize it but I hereby authorize you to also receive 50 points for your answer. Please arrange with Experts-exchange.

By the way, I am still adamant that I had RIGHT aligning MaskEd Boxes at one stage.

Could it be that I had some 3rd party control which I then overwrote with a later version of th control which was left justified? I suppose I'll never know. Once again, please accept my apologies. I never meant to offend you.
0
 
MirkwoodCommented:
It's ok. 50 points is not enough to get upset about. Unlike my answer the apologies are accepted :)
0
 
PeterNAuthor Commented:
Mirkwood, it's not the points at issue - it's the effort you put in. Without people like you, Experts-Exchange could not be the success it is. The more I study your original reply, the worse I feel. No hard feelings I hope? Keep up the good work!

Thanks - PeterN
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now