Wednesday 8 February 2012

Input decimal value in textbox in vb.net


Input decimal value in textbox in vb.net

Private previousText As String
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        previousText = TextBox1.Text
    End Sub
    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        If String.IsNullOrEmpty(DirectCast(sender, TextBox).Text) Then
            previousText = ""
        Else
            Dim num As Double = 0
            Dim success As Boolean = Double.TryParse(DirectCast(sender, TextBox).Text, num)
            If success And num >= 0 Then
                DirectCast(sender, TextBox).Text.Trim()
                previousText = DirectCast(sender, TextBox).Text
            Else
                DirectCast(sender, TextBox).Text = previousText
                DirectCast(sender, TextBox).SelectionStart = DirectCast(sender, TextBox).Text.Length
            End If
        End If
    End Sub

No comments:

Post a Comment