Format TextBox only Numerical Data and 2 decimal places, enable backspace
-
Monday, April 26, 2010 9:48 PM
Hi,
I have managed to format a text box to be formatted to two decimal places, and to only accept numbers.
Only problem is i need to allow the backspace so that if someone types something wrong, they can backspace and type the correct number
Private Sub txtCAmount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCAmount.KeyPress 'Allow only numberical input plus decimal. Dim allowedChars As String = "0123456789." If allowedChars.IndexOf(e.KeyChar) = -1 Then ' Invalid Character e.Handled = True End If End Sub Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCAmount.Leave 'Format to 2 decimal places. txtCAmount.Text = Format(Double.Parse(txtCAmount.Text), "###,###,##0.00") End Sub
Can someone tell me how I could modify above to allow the backspace?Thanks.
All Replies
-
Monday, April 26, 2010 10:15 PM
Will this work?
Dim allowedChars As String = "0123456789." & vbBack If allowedChars.IndexOf(e.KeyChar) = -1 Then ' Invalid Character e.Handled = True End If
v/r LikeToCode....Mark the best replies as answers.- Marked As Answer by superlative Monday, April 26, 2010 10:26 PM
-
Monday, April 26, 2010 10:26 PMPerfect.
-
Monday, April 26, 2010 10:27 PM
LikeToCode's method looks like it'll work for you but just an opinion here (and nothing more:)...
I don't even try to do any of that. If the idiot is dumb enough to type "six" instead of "6", then tell him/her it won't work, but I don't prevent them from doing it.
Instead, consider using Double.TryParse which will return a boolean. In other words, if it successfuly converted it to a double, proceed, but if not, clear the text box, put the focus back in it, then pop up a message indicating that their input wasn't valid.
Food for thought
- Proposed As Answer by _asgar Tuesday, April 27, 2010 9:46 AM
-
Monday, April 26, 2010 10:29 PM
Some of the best number formatting that I know are in this link. Check out in particular, the post by dbasnet:
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/4f84cbd1-4bc1-43af-8fe6-7080c85e02e2
Only performance counts! -
Monday, April 26, 2010 10:36 PM
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) AndAlso Not e.KeyChar = "."c Then e.Handled = True End If End Sub
Asgar -
Monday, April 26, 2010 11:17 PM
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) AndAlso Not e.KeyChar = "."c Then e.Handled = True End If End Sub
AsgarAsgar,
Shouldn't there also be an "Else" with "e.Handled=False", or is that not needed?
Never mind - I see what you're doing now. You're taking care of everything anyone could type on the keyboard so there really IS no "else". -
Tuesday, April 27, 2010 9:46 AM
i THINK YOU HAVE GOT IT BUT YOUR SUGGESTION IS THE BEST.
Not allowing a user to type in the textBox is an irritation to the user.
Asgar

