Monday 5 December 2011

Numeric values in Textbox

'Declaring integer variable globally
Dim dot As Integer 

'Key Press event of TextBox1 Field 


Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
     
If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) = True Or e.KeyChar = "."c Then
                check()
                If dot = 1 And e.KeyChar = "."c Then
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        End If
    End Sub

'Write function named check inside the class

Private Function check()
        Dim str As String = TextBox1.Text
        If str.Contains(".") = True Then
            dot = 1
        Else
            dot = 0
        End If
        Return dot
    End Function

No comments:

Post a Comment