Link to home
Start Free TrialLog in
Avatar of Sharalynn
SharalynnFlag for United States of America

asked on

KeyDown slower by 1 move/key

If txtDOB.Text.Trim.Length = 0 Then
        picDOB.Image = Image.FromFile("error.gif")
Else
        picDOB.Image = Image.FromFile("ok.gif")
End If

With the above in KeyDown method for txtDOB, if i delete the spaces in the textbox, then press a character, it will still display as error, unless I type in another character, which now knows OK.

However if all is in the KeyUp method, all is fine, but if I hold down a letter it will only execute my codes after spamming, which is useless.
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

KeyDown fires before the key is added to the textbox, so that if you want to you can refuse the character.  It how you can do thing like disallow symbols in a textbox without using a mask.

I don't understand why spamming for KeyUp represents a problem-- it they spam a bunch of text the field still isn't valid.  Instead of just checking for at least one character, try this instead on the KeyUp event to validate the field

    If IsDate(txtDOB.Text.Trim) Then
        picDOB.Image = Image.FromFile("ok.gif")
    Else
        picDOB.Image = Image.FromFile("error.gif")
    End If
Avatar of Sharalynn

ASKER

Well because you see, I also need a count of the length of the textbox text so that I can remove the picture when txtDOB.textlength is 0.

So example if I have "a" in my textbox, there is an error. If I press backspace, the a is gone, but there is still an error. If I type "a" again, it will still have the error unless I type another "a" again. This is for KeyDown.

For KeyUp, all works fine, but if the user holds "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" right from the start, no validation is done until he releases up the key. NOT to mention that if there is some validation done and a label is displayed, it would 'lag' out. You understand what I am trying to say?
> Well because you see, I also need a count of the length of the textbox text so that I can remove the picture when txtDOB.textlength is 0.

The code I gave will remove the picture when txtDOB.Text.Length == 0

> So example if I have "a" in my textbox, there is an error. If I press backspace, the a is gone, but there is still an error. If I type "a" again, it will still have the error unless I type another "a" again. This is for KeyDown.

'a' is not a valid date: you *want* it to give an error there.  

> For KeyUp, all works fine, but if the user holds "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" right from the start, no validation is done until he releases up the key. NOT to mention that if there is some validation done and a label is displayed, it would 'lag' out. You understand what I am trying to say?

The code I gave you won't do any validation until the user lets up- no lag.  At that point, 'aaaaaaaaaaaaaaaaaaa' is not valid, which is correct.  Have you tried the code?
Hi, thank you for your replies.

The code you gave did not state textlength=0 and remove the picture. I did not try your code as I have more textboxes that need specific validations including certain string format then I must have. The code that you provide only caters for Date validation only.

So the main thing is, is there any KeyXXX that will register the down button immediately(and the character) and so I can then validate it?
ASKER CERTIFIED SOLUTION
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot to account for the need to .Trim() the string.  This adds some complexity, since the result could depend on the location of the cursor.  However, the premise is still the same.
Thanks, I will take a look at your code. I am not using ErrorProvider as I am not comfortable with it.

You mentioned "You should load the Gifs once and keep them in memory, rather than reading them from file each time.", any examples on that? I am using embedded resource instead of FromFile now.
Embedded resource should be good enough.