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
    forums
Old 02-24-2003, 02:35 PM   #1 (permalink)
Hrqls
Regular Contributor
 
Join Date: May 2002
Location: Alkmaar, the Netherlands
Posts: 167
Hrqls is on a distinguished road
Selective Textboxes

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).
Hrqls is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 08:04 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting