Question Need help to validate Currency and Numeric using KeyPress

Joined
Oct 14, 2009
Messages
4
Programming Experience
1-3
this is my code in VB.NET 2008 and it works :D
as the result, i can input starts from 1.00 until 99.99 :eek:

but i need more than this ;);)
i want the textbox automatic validate the input as i typed in the textbox :confused::confused:

example: i typed "1000" then the textbox will write "1,000"
example: i typed "10000.99" then the textbox will write "10,000.99"
nb: it has to disabled from typing ","

Please help me....!!!! S.O.S....... :confused::confused:

VB.NET:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    e.Handled = Percent(TextBox1, sender, e.KeyChar)
End Sub

Public Function Percent(ByVal tb As TextBox, ByVal sender As System.Object, ByVal eChar As Char) As Boolean
    Dim str As String = tb.Text & eChar
    If Val(str) <= 100 Then
        Dim c As Char = Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator
        Dim chkstr As String = "0123456789" & c
        If chkstr.IndexOf(eChar) > -1 OrElse eChar = vbBack Then
            If tb.Text = "" And chkstr.IndexOf(eChar) = 0 Then
                Return True
            ElseIf tb.Text = "" And chkstr.IndexOf(eChar) = 10 Then
                Return True
            Else
                If eChar = c Then
                    If CType(sender, TextBox).Text.IndexOf(eChar) > -1 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    Dim tt As Integer = InStr(tb.Text, c, CompareMethod.Text)
                    Dim ts As String = Microsoft.VisualBasic.Mid(tb.Text, tt + 1, tb.Text.Length - tt)
                    If tt = 0 Then
                        Return False
                    Else
                        If ts.Length >= 2 Then
                            If eChar = vbBack Then
                                Return False
                            Else
                                Return True
                            End If
                        Else
                            Return False
                        End If
                    End If
                End If
                Return False
            End If
        Else
            Return True
        End If
    Else
        If eChar = vbBack Then
            Return False
        Else
            Return True
        End If
    End If
End Function
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,481
Location
Sydney, Australia
Programming Experience
10+
Validating numeric input as you type is actually far more difficult than it initially seems. You also have to consider pasting. This might give you some ideas:

Numeric Text Box
 
Joined
Oct 14, 2009
Messages
4
Programming Experience
1-3
that one validate when pressing button or maybe i can use on textbox Validated event.
but the one i need is where i can put on textbox Keypress event.
so when i've press any numbers, it changed into currency format.

example: i typed "10000.99" then the textbox will write "10,000.99" or "10.000,99" (depends on the CurrencyDecimalSeparator)

please... somebody... help me.....
S.O.S.......
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,481
Location
Sydney, Australia
Programming Experience
10+
Um, there's a difference between validating and formatting. The link I provided is for a custom control that DOES validate on each key press. I don't see how you could have not seen that if you read the code. Both the NumberBox and SimpleNumberBox classes I posted there override the OnKeyPress method, so that should be a dead giveaway. What you're talking about is formatting, not validating, and my NumberBox class does that too. It obviously doesn't make sense to format until the entire value has been entered, so the formatting is done on the Validating event, which basically means when the control loses focus. Basically, my control does exactly what you want, so you must not have made too much effort to understand it.
 
Top Bottom