Wednesday, November 23, 2011

Numeric Textbox allows only single .

Numeric Textbox
Allows digits only, only single ., limited decimal places

Private Sub txtnProQty_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnProQty.KeyPress, txtnumProAmount.KeyPress, txtnumProCurRate.KeyPress, txtnSerQty.KeyPress, txtnumSerAmount.KeyPress, txtnumSerCurRate.KeyPress
Dim nKey As Integer = Asc(e.KeyChar)
Dim tBox As TextBox = CType(sender, TextBox)
Dim nMaxDecPlaces As Integer = 3
If nKey = 8 Then 'Backspace
e.Handled = False
ElseIf (nKey >= 48 And nKey <= 57) Or nKey = 46 Then
If nKey = 46 AndAlso tBox.Text.Contains(".") Then
e.Handled = True
Else
If (tBox.Text.IndexOf(".") > -1) And ((tBox.Text.Length - 1) - tBox.Text.IndexOf(".")) = nMaxDecPlaces Then
e.Handled = True
Else
e.Handled = False
End If
End If
Else
e.Handled = True
End If
End Sub

Private Sub txtnProQty_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtnProQty.KeyUp, txtnumProAmount.KeyUp, txtnumProCurRate.KeyUp, txtnSerQty.KeyUp, txtnumSerAmount.KeyUp, txtnumSerCurRate.KeyUp
Dim tBox As TextBox = CType(sender, TextBox)
If tBox.Text = "" Then
tBox.Text = "0"
tBox.SelectAll()
End If
End Sub