| :: Floating Point Numbers Validation |

|
Author |
Raju V |
Language |
VB5, VB6 |
Operating
Systems |
Windows 95, 98, NT, Me
and 2k |
| Module |
Option Explicit
' For validating the Floating Point Numbers
' MODULE
Public Sub ValidateDel(txtnum As TextBox, ByVal m_Precision As Integer, KeyCode As Integer)
Dim m_NumBeforeDot As Integer, m_NumAfterDot As Integer
Dim TempInt As Integer
If KeyCode = vbKeyDelete Then
If m_Precision > 0 Then
TempInt = InStr(txtnum, ".")
If TempInt > 0 Then
m_NumBeforeDot = TempInt - 1
m_NumAfterDot = Len(txtnum) - TempInt
Else
m_NumBeforeDot = Len(Trim(txtnum))
m_NumAfterDot = 0
TempInt = txtnum.MaxLength - m_Precision
End If
If (txtnum.SelStart = TempInt - 1) Then
If m_NumBeforeDot + m_NumAfterDot >= txtnum.MaxLength - m_Precision Then
KeyCode = 0
End If
End If
End If
End If
End Sub
Public Sub ValidateNum(txtnum As TextBox, ByVal m_Precision As Integer, KeyAscii As Integer)
Dim m_NumBeforeDot As Integer, m_NumAfterDot As Integer
Dim TempInt As Integer
If (KeyAscii <> vbKeyReturn) And _
((KeyAscii < vbKey0) Or (KeyAscii > vbKey9)) And _
(KeyAscii <> vbKeyBack) Then
If KeyAscii = 46 Then ' Dot.
If m_Precision = 0 Then
KeyAscii = 0
Exit Sub
Else
If InStr(txtnum, ".") > 0 Then
KeyAscii = 0
Exit Sub
End If
End If
Else
KeyAscii = 0
Exit Sub
End If
End If
If KeyAscii = 0 Then Exit Sub
If m_Precision > 0 Then
TempInt = InStr(txtnum, ".")
If TempInt > 0 Then
m_NumBeforeDot = TempInt - 1
m_NumAfterDot = Len(txtnum) - TempInt
Else
m_NumBeforeDot = Len(txtnum)
If txtnum.SelStart < m_NumBeforeDot - m_Precision Then
KeyAscii = 0
Exit Sub
End If
m_NumAfterDot = 0
TempInt = txtnum.MaxLength - m_Precision
End If
If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
If Not (((m_NumBeforeDot < txtnum.MaxLength - m_Precision - 1) _
And (txtnum.SelStart < TempInt)) Or _
((m_NumAfterDot < m_Precision) And (txtnum.SelStart >= TempInt))) Then
KeyAscii = 0
End If
ElseIf (KeyAscii = vbKeyBack) And (txtnum.SelStart = TempInt) Then
If m_NumBeforeDot + m_NumAfterDot >= txtnum.MaxLength - m_Precision Then
KeyAscii = 0
End If
End If
End If
End Sub
|
| Usage |
Private Sub Form_Load()
Text1.Text = Empty
End Sub
' USAGE
'Create a form a text box
Private Sub Text1_KeyPress(KeyAscii As Integer)
' Precision is 2 in this case
' Send the Precision Value same in both the Functions
Call ValidateNum(Text1, 2, KeyAscii)
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Call ValidateDel(Text1, 2, KeyCode)
End Sub
|
|
 |
|
 |