Advertisement

05.01.2008 at 07:42AM PDT, ID: 23368673
[x]
Attachment Details

How to best handle KeyPress and LostFocus

Asked by sasllc in Visual Studio .NET 2005, .NET Framework 2.0

Tags: vb 2005

We have developed a vb 2005 program that runs on a handheld scanner, in the compact framework 2.0 environment.  The user keys "Location C" into a text box and presses Enter, at which time the the program validates the data in the text box and saves it, etc.  The general idea of what I'm doing is below:
=========================================================
Private Sub txtLocC_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocC.KeyPress

        If e.KeyChar = ChrW(13) Then e.Handled = True

 If Not (Len(Me.txtLocC.Text) > 0 And Len(Me.txtLocC.Text) <= 4) Then
                'set up the error text...
                Call LocSub1(piCf1_LocC_Sz)
                GoTo Err
            End If
=======================================================
....etc, etc.  Actually, the sub has quite a few lines of validation code.  But that all works fine, as long as the user presses the Enter key when they are finished keying in data.

The problem is that some users insist on touching the next text box with their finger or stylus when they are finished with this text box, rather than pressing Enter, so none of my validation code is ever being processed.

I'm wondering if the best solution would be for me to use the "LostFocus" concept, to handle cases where the user makes the mistake described above.  From what I can tell--with my limited knowledge--it appears to me that I would essentially have to repeat my entire sub, using different syntax in the first line (where I say Private Sub txtLocC_KeyPress...)  I would prefer to not have to repeat all the code for each of my subs that process the various text boxes, because all of them are quite wordy, and of course in the compact environment I've got to conserve.

So, my question is this: what IS the best way to handle this problem, knowing that the user may press Enter, or they may enter the data and then touch another text box without pressing Enter?  I really need examples of the proper code to use as well.  TIA
Start Free Trial
[+][-]05.01.2008 at 07:55AM PDT, ID: 21479199

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Visual Studio .NET 2005, .NET Framework 2.0
Tags: vb 2005
Sign Up Now!
Solution Provided By: xshiverx
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.01.2008 at 08:28AM PDT, ID: 21479533

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628