Question Set Masktextbox mask to allow 1 or 2 digit after decimal point run time

prasad kulkarni

Well-known member
Joined
Sep 7, 2009
Messages
62
Programming Experience
1-3
Hi to all,

How to set run time Masktextbox mask to allow only 1 or 2 digit(number)
After decimal point , run time in vb.net

if any know , then reply me soon
 
Here is how you can do it with a plain text box. The code section beginning with dot = is what you are looking for.

VB.NET:
   Private Sub TextBox11_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox11.KeyPress
        Dim dot As Integer, ch As String
        If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
        If e.KeyChar = "-" And TextBox11.SelectionStart = 0 Then e.Handled = False 'allow negative number
        If e.KeyChar = "." And TextBox11.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal point

        dot = TextBox11.Text.IndexOf(".")
        If dot > -1 Then            'allow only 2 decimal places
            ch = TextBox11.Text.Substring(dot + 1)
            If ch.Length > 1 Then e.Handled = True 'does not allow any other keypresses
        End If
 
       If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
        If e.KeyChar = Chr(13) Then GetNextControl(TextBox1, True).Focus() 'Enter key moves to next control in Tab order
    End Sub
 
Back
Top