Solved

KeyDown slower by 1 move/key

Posted on 2007-11-29
8
288 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:Sharalynn
  • 5
  • 3
8 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20377402
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
0
 

Author Comment

by:Sharalynn
ID: 20380133
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?
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20381969
> 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?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Sharalynn
ID: 20387139
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?
0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 20395465
This code will remove the picture if there is no text, because an empty string is not a valid date.  But I understand the desire to have a more generic validation method.

In that case, there is no event that will just give you the whole string on key down.  However, you do have easy access to the existing string, and you get a keycode that can be translated and applied to the existing string:

    Private Sub KeyDownValidation(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        'first get easy to use textbox reference
        Dim box As TextBox = DirectCast(sender, TextBox)
        Dim length As Integer = box.Text.Length

        'then find out how this character will effect the length
        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then
            length -= 1
        ElseIf box.Multiline = False AndAlso (e.KeyCode = Keys.Enter OrElse e.KeyCode = Keys.Return) Then
            'if it's a single line text box, enter/return have no effect- do nothing
        Else
            Dim s As String = Chr(e.KeyValue)
            s.Trim()
            length += s
        End If

        '  Create a function to find the picture box based on the name of the textbox
        '  should be easy enough as long as you follow good naming conventions
        Dim pic As PictureBox = Nothing
        'pic = GetPictureBoxByName(box.Name)

        'now we can validate the result
        If length >= 1 Then
            'validation succeeds
            pic.Image = Image.FromFile("ok.gif")
        Else
            'validation fails
            pic.Image = Image.FromFile("error.gif")
        End If
    End Sub

Some things to note here:  
    * You link that method to your textboxes using the AddHandler keyword.  
    * I left implementing GetPictureBoxByName() to you, but it should be a fairly simple thing to do.
    * You should load the Gifs once and keep them in memory, rather than reading them from file each time.
    * Have you looked at the ErrorProvider component?  It will do most of this for you.

0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20395708
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.
0
 

Author Comment

by:Sharalynn
ID: 20395899
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.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20396093
Embedded resource should be good enough.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now