Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 137,219 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,230 people online right now. Registration is fast and FREE... Join Now!





Use KeyChar to limit the characters that can be entered into a Text Box

A method to allow the user to enter only certain specific characters into a text box and ignore any other key. In this example only numbers, the Backspace key and the period will be allowed, everything else is ignored.

Submitted By: Jayman
Actions:
Rating:
Views: 12,174

Language: VB.NET

Last Modified: August 9, 2006
Instructions: Put this code inside the KeyPress event for any text box that you want to limit user input.

Snippet


  1. 'allow only numbers, the Backspace key and the period
  2.  
  3. If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
  4.     AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
  5.     'cancel keys
  6.     e.Handled = True
  7. End If

Copy & Paste


Comments


jagatworld 2008-05-16 23:18:21

Thanks, i am using this. Actually i used it way long back ofcourse, but now, your code came handy to my project. Regds.

Mommy 2008-08-05 18:28:19

Works like a charm ;)


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month