Numeric Only TextBox

It's Very Easy to make a Numeric only TextBox ..
Just right the Following Code on The Textbox's KeyPress Event..

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar > 31 && (e.KeyChar < '0' || e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }


Above Code Wont Let you enter anything other than 0 to 9 Numerics

To Limit the Number of Character in Texbox use the MaxLength Property  of Texbox

Example:

TextBox1.MaxLength = 10;


Vb.net

 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar > "31" And (e.KeyChar < "0" Or e.KeyChar > "9") Then
            e.Handled = True
        End If
    End Sub


Example:

TextBox1.MaxLength = 10

posted @ 2012-04-01 14:53  sandeepparekh9  阅读(142)  评论(0编辑  收藏  举报