?
Solved

MaskedEdit box right justify

Posted on 1999-07-23
12
Medium Priority
?
1,279 Views
Last Modified: 2008-02-20
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
Comment
Question by:PeterN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1527016
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
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1527017
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
 

Author Comment

by:PeterN
ID: 1527018
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1527019
Nope, no version of the maskeditbox has EVER supported right alignment. So either you changed the masked or used another edit control.
0
 

Author Comment

by:PeterN
ID: 1527020
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
 
LVL 8

Expert Comment

by:vettranger
ID: 1527021
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
 

Author Comment

by:PeterN
ID: 1527022
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
 
LVL 8

Accepted Solution

by:
vettranger earned 150 total points
ID: 1527023
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
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1527024
I reject to the fact that my answers were rejected and I wouldn't have rejected the points :)
0
 

Author Comment

by:PeterN
ID: 1527025
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
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1527026
It's ok. 50 points is not enough to get upset about. Unlike my answer the apologies are accepted :)
0
 

Author Comment

by:PeterN
ID: 1527027
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question