Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    vb
  » Selective Textboxes
      by Hrqls
 Page 1 of 1 
   

(Login to remove green text ads)
Ever had a textbox on your visual basic form, but only wanted to accept numeric data ?
Well, you can do that as follows :

To test the code: open a form, add a textbox to it, and put in the following code.
In the KeyPress event of the textbox put the following code

Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 8 'Backspace is allowed Case 44, 46 'Comma and dot are allowed If InStr(Text1.Text, ".") Then 'When there is already a dot, then KeyAscii = 0 'dont accept the dot Else 'otherwise KeyAscii = 46 'place a dot (also when the input was a comma) End If Case 48 To 57 'nunmbers are allowed Case Else KeyAscii = 0 'the rest is not accepted End Select End Sub
This code can simply be changed so it only accepts lowercase chars:


Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 8 'Backspace is allowed Case 65 To 90 'Uppercase chars changed to lowercase KeyAscii = KeyAscii - 32 Case 97 To 122 'Lowercase chars are allowed Case Else KeyAscii = 0 'the rest is not accepted End Select End Sub
As you can see all you have to do is to change the cases to make sure the right keys are allowed and the rest is ignored (or changed into something which is allowed).




 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle