 |
|
 |
Change...
if(!IsNumeric(KeyCode))
{
e.Handled=true;
return;
}
for...
if (!IsNumeric(KeyCode))
{
e.Handled = true;
if ((KeyCode >= 96) && (KeyCode <= 105)) { KeyCode -= 48; }
else { return; }
}
|
|
|
|
 |
|
 |
I added the Num-Lock pad code and a line to remove leading Zeros in VB net
Private str As String = ""
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Dim KeyCode As Integer = e.KeyValue
If Not IsNumeric(KeyCode) Then
e.Handled = True
If (KeyCode >= 96) AndAlso (KeyCode <= 105) Then
KeyCode -= 48
Else
Return
End If
Else
e.Handled = True
End If
If ((KeyCode = 8) OrElse (KeyCode = 46)) AndAlso (str.Length > 0) Then
str = str.Substring(0, str.Length - 1)
ElseIf Not ((KeyCode = 8) OrElse (KeyCode = 46)) Then
str = str & Convert.ToChar(KeyCode)
End If
If str.Length = 0 Then
TextBox1.Text = "0.00"
ElseIf str.Length > 3 And str.Substring(0, 1) = 0 Then
str = str.Substring(1, str.Length - 1)
End If
If str.Length = 1 Then
TextBox1.Text = "0.0" & str
ElseIf str.Length = 2 Then
TextBox1.Text = "0." & str
ElseIf str.Length > 2 Then
TextBox1.Text = str.Substring(0, str.Length - 2) & "." & str.Substring(str.Length - 2)
End If
End Sub
Private Function IsNumeric(ByVal Val As Integer) As Boolean
Return ((Val >= 48 AndAlso Val <= 57) OrElse (Val = 8) OrElse (Val = 46))
End Function
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = True
End Sub
|
|
|
|
 |
|
 |
In my project,I used your code for my currency textbox.I also created a button which will clear the textbox(textbox.Text = "").When I run the program,your code do work but when I cleared the textbox (pressed the clear button) and typed another value,the value I typed will concatenate to the previous value.
For example: I typed 175.25 and then pressed the clear button.The textbox is empty and then I typed another value let's say 35.The value that will appear in the textbox is 17525.35.
Any idea about this?
|
|
|
|
 |
|
 |
My apologies! I got it.I just missed to empty the value of the str variable.thanks for the code anyway.
|
|
|
|
 |
|
 |
' This is VB.net version, similar to the original posting, but with a few additional ' lines of code added. ' Compliments given to the author. Private str As String = "" Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown Dim keycode As Integer = e.KeyValue If Not IsNumeric(keycode) Then e.Handled = True Return Else e.Handled = True End If If ((keycode = 8 Or keycode = 46) And str.Length > 0) Then str = str.Substring(0, str.Length - 1) Else If Not (keycode = 8 Or keycode = 46) Then ' comment: I added these lines to include keys captured with numlock on If (keycode >= 96 And keycode <= 105) Then keycode -= 48 End If str &= Convert.ToChar(keycode) End If End If If str.Length = 0 Then textbox1.Text = "" ElseIf str.Length = 1 Then textbox1.Text = "0.0" & str ElseIf str.Length = 2 Then textbox1.Text = "0." & str ElseIf str.Length > 2 Then ' comment: ommit character 0 in the front If str.Length >= 4 And str.Substring(0, 1) = "0" Then str = str.Substring(1) End If textbox1.Text = str.Substring(0, str.Length - 2) & "." & str.Substring(str.Length - 2) End If End Sub Private Function isNumeric(ByVal val As Integer) As Boolean ' comment: added value range from 96 to 105 (numlock) Return ((val >= 48 And val <= 57) Or (val >= 96 And val <= 105) Or (val = 8) Or (val = 46)) End Function Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox1.KeyPress e.Handled = True End Sub
|
|
|
|
 |
|
|
 |
|
 |
Saved me the time it would have took to write it. Thanks.
|
|
|
|
 |
|
 |
Speaking of "additional stupidity". Let's look at the event handlers for this article for a second, here they are.
private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
Now, at first glance I see the words System.Windows.Forms, not System.Web.UI.
If the article was based on a asp.net textbox your comments would be valid, but since it is not your comments are complete irrelavant. (No postbacks in windows forms).
I'm not going to say that I don't find it odd to have this article on code project, but it could be relevant to people just starting in .net WINDOWS FORMS.
Before you go trashing an article maybe you should read it more closely.
|
|
|
|
 |
|
 |
Good call. Apologies extended and erronious comment deleted. Tragically, ASP.NET handlers look exactly like Windows.Forms handlers, and tend to get misused because of this. Apparently misidentified as well.
-Bob Aman
|
|
|
|
 |
|
 |
Can I moan, groan, and complain about bad coding style instead?
-Bob Aman
|
|
|
|
 |
|
 |
As not everyone use a dot as the decimal separator, I strongly suggest that uou use System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator instead of "." in your code.
As Everyone had to do that "tricky" code once at least, that's rather cool to see it on a codeproject article.
Michael CARBENAY
|
|
|
|
 |
|
 |
delete this post since you double posted.
IM PROUD TO BE A GMAIL;
|
|
|
|
 |
|
 |
a mess. No format at all.
|
|
|
|
 |