1,105,229 Community Members

Formatting Textbox As You Type

Member Avatar
Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 
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?

Member Avatar
kinwang2009
Posting Whiz in Training
243 posts since Feb 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
1
 

Then try using

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

Hope this helps

Member Avatar
kinwang2009
Posting Whiz in Training
243 posts since Feb 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
0
 

Then try using

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

Hope this helps

Member Avatar
kinwang2009
Posting Whiz in Training
243 posts since Feb 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
0
 

Then try using

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

Hope this helps

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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
Member Avatar
Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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?

Member Avatar
Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes please.

Member Avatar
ryan311
Posting Whiz in Training
294 posts since Jul 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

use maskedbox component.

Member Avatar
Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.

Member Avatar
labq5
Newbie Poster
11 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
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.

Member Avatar
Shinehah
Newbie Poster
23 posts since May 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.

Member Avatar
gelaisg18
Newbie Poster
6 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

hi there i just have a problem with my program i used the keycodes of vb wherein every time i enter a letter from A-Z it will display each letter together with a special character " | ". Im done with this but my problem is, the letters were displaying from right to left. please help me with this.

here is my code:

If KeyAscii = 13 Or KeyAscii = 9 Then
fname_txt.SetFocus
surname_txt.Text = StrConv(surname_txt.Text, 1)
End If

If KeyAscii <= 90 Or KeyAscii >= 65 Then
surname_txt.Text = Format(" " & "|" & " " & surname_txt.Text & " ")
End If

Thanks in advance!

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

@gelaisg18, this post is already 2 YEARS old, please open your own thread with your question, code and problem. We will gladly help from there, thanx.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

My bad, just saw your new thread, I will reply from there.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: