Allow Numbers only in a TextBox - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This code is used to set a restriction on a textbox and allow only number to be entered in Visual Basic .NET

allow number only in a textbox visual basic .net

The code will be added to the TextBox KeyPress event. In other words, each time the user hit a key to be entered in a textbox, the program will check the key pressed and make sure it is a number

The code

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Please enter numbers only")
            e.Handled = True
        End If
    End Sub

allow only numbers in visual basic 2013

You can also remove all the numbers from a textbox:

Share This

Home | About | Contact | Privacy Policy

Copyright 2018 - All Rights Reserved.