TextBox with comma formatting

This is a discussion on TextBox with comma formatting within the basic.visual forums in Programming Languages category; Hi All I wish to allow a user to input numbers of up to a million or more. Simply displayed in a TextBox they're nearly incomprehensible, think 10000000 against 10,000,000. What I would like to achieve :- 1. If the user inputs numeric characters simply add them to the TextBox but display them in thousands comma format. 2. If non-numeric then a Beep and ignored 3. However, to allow the user to use CNTR+C, CNTRL+V and allow insertions and so on. Seems to me that would need oceans of code. Not posting code 'cos six or more attempts failed so ...

Go Back   Application Development Forum > Programming Languages > basic.visual

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-05-2008, 11:45 AM
Martin Trump
Guest
 
Default TextBox with comma formatting

Hi All

I wish to allow a user to input numbers of up to a million or more.
Simply displayed in a TextBox they're nearly incomprehensible, think
10000000 against 10,000,000.

What I would like to achieve :-

1. If the user inputs numeric characters simply add them to the TextBox
but display them in thousands comma format.

2. If non-numeric then a Beep and ignored

3. However, to allow the user to use CNTR+C, CNTRL+V and allow
insertions and so on. Seems to me that would need oceans of code.

Not posting code 'cos six or more attempts failed so badly, I wouldn't
know which to post.

Help appreciated, TIA
..
--
Martin Trump
Reply With Quote
  #2  
Old 09-05-2008, 03:02 PM
Ralph
Guest
 
Default Re: TextBox with comma formatting


"Martin Trump" <martin@wmeadow.demon.co.uk> wrote in message
news:XuLFmGDAQVwIFwKV@wmeadow.demon.co.uk...
> Hi All
>
> I wish to allow a user to input numbers of up to a million or more.
> Simply displayed in a TextBox they're nearly incomprehensible, think
> 10000000 against 10,000,000.
>
> What I would like to achieve :-
>
> 1. If the user inputs numeric characters simply add them to the TextBox
> but display them in thousands comma format.
>
> 2. If non-numeric then a Beep and ignored
>
> 3. However, to allow the user to use CNTR+C, CNTRL+V and allow
> insertions and so on. Seems to me that would need oceans of code.
>
> Not posting code 'cos six or more attempts failed so badly, I wouldn't
> know which to post.
>
> Help appreciated, TIA
> .


A Masked Edit control should work for you. Don't use VB's Google about for a
replacement. Here is a nice one...
http://www.freevbcode.com/ShowCode.Asp?ID=2299

As for your last question, it sort of depends on what your 'default' is when
copying. eg, whether you want a User to highlight and copy "10,000,000" or
"10000000"?

I have often simply provided a small clipboard button to the side which
automatically copys the 'format' or the 'data', or brings up a small dialog
allowing the user to chose. It depends really on how often copy 'n paste is
done.

[You might even want to define your own custom clipboard format.

http://www.vbaccelerator.com/home/VB...se/article.asp
]

-ralph


Reply With Quote
  #3  
Old 09-06-2008, 12:33 PM
Martin Trump
Guest
 
Default Re: TextBox with comma formatting

Ralph

>A Masked Edit control should work for you.


Thanks for your posting. Since I posted I've found an adequate, but not
perfect solution.

There are Text1 and Text2, the former the comma formatted display, the
latter the input .

Using KeyPreview = True

==============================
Private Sub Form_KeyPress(KeyAscii As Integer)

If KeyAscii = 8 Then 'Back space
Text1.Text = Format$(Left$(Text2.Text, Len(Text2.Text) - 1),
"#,#")
Text2.SelStart = 999
Exit Sub
End If
If (KeyAscii < 48) Or (KeyAscii > 59) Then
KeyAscii = 0
Beep
Else
Text1.Text = Format$(Text2.Text & Chr$(KeyAscii), "#,#")
End If

End Sub
==============================

Thanks again.

Regards

--
Martin Trump
Reply With Quote
  #4  
Old 09-06-2008, 01:02 PM
Steve Gerrard
Guest
 
Default Re: TextBox with comma formatting

Martin Trump wrote:
> Ralph
>
> Thanks for your posting. Since I posted I've found an adequate, but
> not perfect solution.
>


Here is the sort of thing I like to do:

Private Sub Form_Load()
Text1.Text = "0"
End Sub

Private Sub Text1_GotFocus()
If IsNumeric(Text1.Text) Then
Text1.Text = Format(Text1.Text, "0")
End If
End Sub

Private Sub Text1_LostFocus()
If IsNumeric(Text1.Text) Then
Text1.Text = Format(Text1.Text, "#,##0")
End If
End Sub

Private Sub Text1_Change()
If IsNumeric(Text1.Text) Then
Text1.BackColor = vbWindowBackground
Else
Text1.BackColor = vbYellow
End If
End Sub


Reply With Quote
  #5  
Old 09-07-2008, 02:07 PM
Martin Trump
Guest
 
Default Re: TextBox with comma formatting

In message <7YSdnXuxCMppK1_VnZ2dnUVZ_j2dnZ2d@comcast.com>, Steve Gerrard
<mynamehere@comcast.net> writes
>Here is the sort of thing I like to do:


Thanks Steve. All in one TextBox. Wow! I'll give a whirl.

Martin
--
Martin Trump
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:23 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.