Results 1 to 5 of 5

Thread: VB Validation

  1. #1
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    VB Validation

    Ive been trying for hours but can't get what im about to explain to work so ive resorted to asking on here.

    My problem is that I want a text box to only accept any one of the following values A, B, C, D. Any other key that is pressed simply isnt entered into the textbox. Since im not all that clued up with VB im not too sure how its done. Any help would be appreciated.

  2. #2
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    I did something like this before... I can't be specific about the exact code, since I haven't touched VB in ages, but I can (try and) give you the general gist.

    I created a function for when the text box changes. It took the value of the text, and checked it against the values it should be, using an if statement (or a switch/select, whatever it is called). If it wasn't the correct value, you can set textBox.text to a blank value. This probably won't work, but it would be something like this:

    Code:
    function textBox changes                 (Should be pretty obvious what this is suppsed to be)
         if textBox.text = "A" then
              do whatever
         else
              textBox.text = ""
         endif
    If it already has a value in, you let the user add a character, and save the current string. You look at the right most character, and if it is a valid character, it replaces the current value, otherwise, it uses the existing value. e.g.

    Code:
    function textBox changes                 (Should be pretty obvious what this is suppsed to be)
    
         Dim rightChar as string                     (Can't remember the data types)
    
         rightChar = right(textBox.text,1)
    
         if rightChar = "A" then
              textBox.text = rightChar
         else
              textBox.text = left(textBox.text,1)
         endif
    Hope that helps! If that isn't what you wanted, feel free to shout at me. No doubt there's a far better way of doing it as well.
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    you using VB6, or VB.net?

    in vb.net its much nicer way of doing this.
    throw new ArgumentException (String, String, Exception)

  4. #4
    Senile Member
    Join Date
    Dec 2003
    Posts
    442
    Thanks
    3
    Thanked
    0 times in 0 posts
    I haven't done vb in years so this might not be exactly right but it works I used it a few times.

    you want to use the onkeydown (or could it be onkeypress) event and set the keycode to '' (or is it zero, cant remember) if its not one of your desired ones.

    If you only want a single character then make the maxlength 1.


    Actually if thats what you want then just use a dropdown, not a textbox!

  5. #5
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)
    Thanks for your input guys. I decided to go back to a dropdown box as i had before. Only reason i changed to a text box was because you could still type things into the text area part of the dropbox before you drop the menu down. How can that be prevented? Locking or disableing the dropbox makes it useless for the user.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Validating text entered into a VB text box
    By Dorza in forum Software
    Replies: 9
    Last Post: 09-06-2006, 10:15 AM
  2. database and VB help
    By s4ch117 in forum Software
    Replies: 8
    Last Post: 09-02-2004, 01:41 PM
  3. Does VB support animated Gifs?
    By Dorza in forum Software
    Replies: 3
    Last Post: 25-01-2004, 05:15 PM
  4. How the hell do I do this in VB?
    By Steve in forum Software
    Replies: 7
    Last Post: 22-12-2003, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •