VB textbox numbers only

In VB you can allow a textbox to accept numbers only by placing some codes in the KeyPress event of the textbox. The e argument has a property called KeyChar that you can use to read a character from keyboard. You will use Asc(char value) or AscW(char value) to convert the character to integer code. Key 0 to 9 is equivalent to code 48 to 57.

   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

     If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Then ' accept numbers only
        Exit Sub
        Beep() 'sound effect
        e.Handled = True ' do not accept any key other than numbers
     End If
  End Sub

Posted by: Lavy | post date: 02-10-2013 | Subject: VB.NET Programming Language

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.