small routine to check for decimal places

C

Craig G

i have a small routine which i used in VB6 to check whether or not a field is off numeric input

but im having a problem when trying to change this over to .NET

what do i use instead of KeyAscii = 0 at the end of the routine? or how can i modify it to work in .NET??

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

IsNumericInput(TextBox1, 2, Asc(e.KeyChar))

End Sub



Public Sub IsNumericInput(ByVal Ctl As System.Windows.Forms.TextBox, ByVal iDP As Integer, ByVal KeyAscii As Integer)

On Error GoTo ThisIsNotANumber

Dim iLen As Integer, sText As String, nCnt As Integer

Dim sUseDP As String

Const D_POINT = 46

Const SDP = 44

If InStr(Format(9.9, "#.#"), ",") > 0 Then sUseDP = "," Else sUseDP = "."

sText = Ctl.Text

iLen = Len(Ctl.Text)

If KeyAscii = 8 Then Exit Sub

If InStr("1234567890" & sUseDP, Chr(KeyAscii)) = 0 Or (iDP = 0 And ((KeyAscii = D_POINT And sUseDP = ".") Or (KeyAscii = SDP And sUseDP = ","))) Then GoTo ThisIsNotANumber

If iDP = 0 Then

If Ctl.Text = "" And iLen = Ctl.MaxLength Then GoTo ThisIsNotANumber

Exit Sub

End If

If Ctl.SelectedText = "" Then

If iLen = Ctl.MaxLength Then GoTo ThisIsNotANumber

If InStr(sText, sUseDP) > 0 And ((KeyAscii = D_POINT And sUseDP = ".") Or (KeyAscii = SDP And sUseDP = ",")) Then GoTo ThisIsNotANumber

If InStr(sText, sUseDP) = 0 Then

nCnt = iLen

Else

nCnt = InStr(sText, sUseDP) - 1

End If

If nCnt = Ctl.MaxLength - iDP - 1 And Ctl.SelectionStart <= nCnt And KeyAscii <> D_POINT Then GoTo ThisIsNotANumber

If InStr(sText, sUseDP) > 0 Then

nCnt = iLen - InStr(sText, sUseDP)

If nCnt = iDP And Ctl.SelectionStart > InStr(sText, sUseDP) Then GoTo ThisIsNotANumber

End If

Else

If (KeyAscii = D_POINT And sUseDP = ".") Or (KeyAscii = SDP And sUseDP = ",") Then

If InStr(sText, sUseDP) > 0 And InStr(Ctl.SelectedText, sUseDP) = 0 Then GoTo ThisIsNotANumber

Else

If InStr(Ctl.Text, sUseDP) > 0 Then

nCnt = iLen - Ctl.SelectionLength

If nCnt >= Ctl.MaxLength - iDP - 2 Then GoTo ThisIsNotANumber

End If

End If

End If

Exit Sub

ThisIsNotANumber:

KeyAscii = 0

End Sub
 
R

Robin Tucker

There is a much better way than this: use Double.TryParse. Eg. :


Dim theValue as Double

If theValue.TryParse(TextBox.Text, Globalization.NumberStyles.Float,
Application.CurrentCulture.NumberFormat, theValue) = True Then

' Valid number (according to current culture)

else

' Not a valid number

end if


i have a small routine which i used in VB6 to check whether or not a field
is off numeric input

but im having a problem when trying to change this over to .NET

what do i use instead of KeyAscii = 0 at the end of the routine? or how can
i modify it to work in .NET??

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
IsNumericInput(TextBox1, 2, Asc(e.KeyChar))
End Sub

Public Sub IsNumericInput(ByVal Ctl As System.Windows.Forms.TextBox, ByVal
iDP As Integer, ByVal KeyAscii As Integer)
On Error GoTo ThisIsNotANumber
Dim iLen As Integer, sText As String, nCnt As Integer
Dim sUseDP As String
Const D_POINT = 46
Const SDP = 44
If InStr(Format(9.9, "#.#"), ",") > 0 Then sUseDP = "," Else sUseDP = "."
sText = Ctl.Text
iLen = Len(Ctl.Text)
If KeyAscii = 8 Then Exit Sub
If InStr("1234567890" & sUseDP, Chr(KeyAscii)) = 0 Or (iDP = 0 And
((KeyAscii = D_POINT And sUseDP = ".") Or (KeyAscii = SDP And sUseDP =
","))) Then GoTo ThisIsNotANumber
If iDP = 0 Then
If Ctl.Text = "" And iLen = Ctl.MaxLength Then GoTo ThisIsNotANumber
Exit Sub
End If
If Ctl.SelectedText = "" Then
If iLen = Ctl.MaxLength Then GoTo ThisIsNotANumber
If InStr(sText, sUseDP) > 0 And ((KeyAscii = D_POINT And sUseDP = ".") Or
(KeyAscii = SDP And sUseDP = ",")) Then GoTo ThisIsNotANumber
If InStr(sText, sUseDP) = 0 Then
nCnt = iLen
Else
nCnt = InStr(sText, sUseDP) - 1
End If
If nCnt = Ctl.MaxLength - iDP - 1 And Ctl.SelectionStart <= nCnt And
KeyAscii <> D_POINT Then GoTo ThisIsNotANumber
If InStr(sText, sUseDP) > 0 Then
nCnt = iLen - InStr(sText, sUseDP)
If nCnt = iDP And Ctl.SelectionStart > InStr(sText, sUseDP) Then GoTo
ThisIsNotANumber
End If
Else
If (KeyAscii = D_POINT And sUseDP = ".") Or (KeyAscii = SDP And sUseDP =
",") Then
If InStr(sText, sUseDP) > 0 And InStr(Ctl.SelectedText, sUseDP) = 0 Then
GoTo ThisIsNotANumber
Else
If InStr(Ctl.Text, sUseDP) > 0 Then
nCnt = iLen - Ctl.SelectionLength
If nCnt >= Ctl.MaxLength - iDP - 2 Then GoTo ThisIsNotANumber
End If
End If
End If
Exit Sub
ThisIsNotANumber:
KeyAscii = 0
End Sub
 
C

Craig G

infact its not good at all!!
compact framework doenst have a TryParse method for textboxes :blush:(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top