Monday, September 7, 2009

Text Box To Accept Numbers Upto 4 Decimal

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

button1.KeyPress

Try

Dim KeyAscii As Short

= Asc(e.KeyChar)

Dim iAfterDecimal As

Int16 = 0

If CType(sender, TextBox).SelectionStart > txtFillTank.Text.IndexOf(".") AndAlso Not txtFillTank.Text.IndexOf(".") = -1

Then

iAfterDecimal = txtFillTank.Text.Length - (txtFillTank.Text.IndexOf(

"."

))

End

If

If (KeyAscii >= Keys.D0 And KeyAscii <= Keys.D9 And iAfterDecimal <= 4) Or KeyAscii = System.Windows.Forms.Keys.Back

Then

e.Handled =

False

ElseIf KeyAscii = Keys.Delete

Then

If txtFillTank.Text <> String.Empty

Then

If txtFillTank.Text.Contains(".")

Then

e.Handled =

True

Else

e.Handled =

False

End

If

End

If

Else

e.Handled =

True

End

If

Catch ex As

Exception

End

Try

End

Sub