954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Formatting Textbox As You Type

Private Sub Text1_Change()
    Text1 = Format(Text1, "#,###")
    Text1.SelStart = Len(Text1.Text)
End Sub

This code formats the textbox value to include a comma for every 3 digits to the left. Problem is, it doesn't allow to type decimals like 3,000,000.50

What can I do?

Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 14
Solved Threads: 5
 

Then try using

1.
      Private Sub Text1_Change()
      Text1 = Format(Text1, "#,##0.00")
      Text1.SelStart = Len(Text1.Text)
      End Sub


Hope this helps

kinwang2009
Posting Whiz in Training
243 posts since Feb 2010
Reputation Points: 17
Solved Threads: 42
 

Then try using

Private Sub Text1_Change()
      Text1 = Format(Text1, "#,##0.00")
      Text1.SelStart = Len(Text1.Text)
      End Sub


Hope this helps

kinwang2009
Posting Whiz in Training
243 posts since Feb 2010
Reputation Points: 17
Solved Threads: 42
 

Then try using

Private Sub Text1_Change()
      Text1 = Format(Text1, "#,##0.00")
      Text1.SelStart = Len(Text1.Text)
      End Sub


Hope this helps

kinwang2009
Posting Whiz in Training
243 posts since Feb 2010
Reputation Points: 17
Solved Threads: 42
 

Although Kinwang is 100% correct, it will NOT work under the Change event, rather use the LostFocus event.

Private Sub Text1_LostFocus()

Text1 = Format(Text1, "#,##0.00")
Text1.SelStart = Len(Text1.Text)
End Sub


When you need to return the value again use -

dim xValue As Integer

xValue = CDbl(Text1.Text) 'This will remove the commas and see the valua as an integer
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

I already have a LostFocus event that formats the amount but what I really need is the Change event so that it formats the amount as you type. Unfortunately, what Kinwang suggested doesn't work.

Any other ideas? Thanks by the way.

Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 14
Solved Threads: 5
 

Your problem is that your cursor will be placed at the end of the last two digits in the change event. This is exactly what you do not need. I know there is code to manipulate the cursor, but do you really want to go that route?

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Yes please.

Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 14
Solved Threads: 5
 

use maskedbox component.

ryan311
Posting Whiz in Training
254 posts since Jul 2008
Reputation Points: 3
Solved Threads: 5
 

Which specific component do I need to add? I can't find maskedbox. Thank you!

Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 14
Solved Threads: 5
 

Hi Shinehah, Sorry that I am only replying to you now. I have tried several ways to manipulate the cursor, but I think that it will be prone to errors. I have used the masked edit control before, which I see Ryan suggested as well.

On your components tab, right click to add the component, select "Microsoft Masked Edit Control" and add the control to your form. Here are some properties that you will find helpfull with the control, you'll see that it is very similar to a textbox control, just controlling what gets entered into the control.

To clear the Text property when you have a mask defined, you first need to set the Mask property to an empty string, and then the CtlText property to an empty string -

MaskedEdit1.Mask = ""
MaskedEdit1.Text = ""


On your form load event, you can set the Mask property (What You See) to the following-

MaskedEdit1.Mask = "#,###.##" 'The # character allows for only numbers to be added as to where the questionmark (?) allows for only text. 
'So, if you want to add a dollar sign ($) in front of the amount you will use it as -
MaskedEdit1.Mask = "?#,###.##"


You can learn much more on the control at the following MS vb6 site - http://msdn.microsoft.com/en-us/library/11405hcf(VS.71).aspx

I hope that this solves your problem, good luck.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 
labq5
Newbie Poster
11 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Labq5, please read all the posts and you will see that we have already covered all possible string functions. When formatting in the change event, the cursor can not be controlled with the format chosen, resulting in incorrect data entered into the textbox.

The maskededit is the only control that can be utilized in this concept.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Andre I tried working with the Mask Control as you suggested. These are some issues I encountered.
- It can not be aligned right justified.
- If I define mask as "#,###.##" it doesn't allow amount greater than 9,999.99 or if I define "#,###,###.##" and I need an amount less than the format it leaves extra commas that I do not need. It allows to type decimals only when you filled up the entire mask or format.

I'm trying to work on a different approach using keypress or keyup. I'll see what I can do.

Thanks for all your help!

Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 14
Solved Threads: 5
 

It's only a pleasure. Follow the following links as well, I'm sure you will find your solution on one of these pages -

http://msdn.microsoft.com/en-us/library/9k0zysxk(v=VS.71).aspx ,
http://msdn.microsoft.com/en-us/library/bw6xyf3b(v=VS.71).aspx ,

and especially -

http://msdn.microsoft.com/en-us/library/59bz1f0h(v=VS.71).aspx

If this helped, please mark as solved, and good luck in your coding.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: