?
Solved

msgbox triggered last control object

Posted on 2003-03-04
8
Medium Priority
?
152 Views
Last Modified: 2013-12-26
I have a form to accept some input from the user. a textbox which allow user to keep in idno has a keyup even to capture the enter key. after the user press enter, a validateIDNO function will verify the idno and return true or false. when return false, a msgbox will appear with a OK button.  Afterwhich the focus goes back to the textbox.

Private Sub tbxIDNO_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbxIDNO.KeyUp
if e.KeyCode = Keys.Enter Then
   if (validateIDNO(tbxIDNO.text)) then
      'go to another form
   else
      msgbox("Invalid IDNO")
   end if
   e.handled = true
end if
end sub

this seem to work fine. however when a invalid idno is inputed, the msgbox pop up. when I press enter key to close the msgbox. the event handler will capture the enter key and verify the idno again. which will again show me a pop up box with message "Invalid IDNO".

my program assume the user doesn't have a mouse and can only use the keyboard to work. anyone know how to solve this problem?  the program should allow the user to key in the idno again after closing the msgbox.
0
Comment
Question by:MadBeef
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:Chaya
ID: 8064368
I didn't quite understand what your problem is:
Do you want the validateIDNO function to be called again when the msgbox "OK" is clicked?
Anyway to bring the focus back to the textbox you simply write:
text1.setFocus
If you want to cancel the last keypress (the enter key) then set keyAscii=0 or keyCode =0
0
 

Author Comment

by:MadBeef
ID: 8068693
sorry for not making it clear in my question. I do want the focus to be back in my textbox. however when I press 'enter' to close the msgbox(I cannot use mouse as program are build to run without using mouse). the key stroke will be handled by the textbox's keyup event again. so it became a loop which doesn't end. what I wanted is the focus to go back to the textbox so the user can re-enter his IDNO.

here are some sample code. try running it and you will see what I mean.

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Enter Then

            If validateIDNO(TextBox1.Text) Then
                'do something
            Else
                MsgBox("invalid IDNO, please re-enter your idno(focus will go back to textbox now)")
            End If
        End If
        e.Handled = True
    End Sub
    Private Function validateIDNO(ByVal myStr As String) As Boolean
        Return False
    End Function
0
 

Author Comment

by:MadBeef
ID: 8068793
sorry for not making it clear in my question. I do want the focus to be back in my textbox. however when I press 'enter' to close the msgbox(I cannot use mouse as program are build to run without using mouse). the key stroke will be handled by the textbox's keyup event again. so it became a loop which doesn't end. what I wanted is the focus to go back to the textbox so the user can re-enter his IDNO.

here are some sample code. try running it and you will see what I mean.

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Enter Then

            If validateIDNO(TextBox1.Text) Then
                'do something
            Else
                MsgBox("invalid IDNO, please re-enter your idno(focus will go back to textbox now)")
            End If
        End If
        e.Handled = True
    End Sub
    Private Function validateIDNO(ByVal myStr As String) As Boolean
        Return False
    End Function
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:MadBeef
ID: 8069201
I think I have found a solution to this problem using a flag to indicate changes to the textbox. I use TextBox1.Tag as the flag here.

   Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
       If e.KeyCode = Keys.Enter AND TextBox1.Tag = 0 Then
           TextBox1.Tag = 1
           If validateIDNO(TextBox1.Text) Then
               'do something
           Else
               MsgBox("invalid IDNO, please re-enter your idno(focus will go back to textbox now)")
           End If
       Else
           TextBox1.Tag = 0
       End If
       e.Handled = True
   End Sub
   Private Function validateIDNO(ByVal myStr As String) As Boolean
       Return False
   End Function
0
 

Expert Comment

by:Chaya
ID: 8070251
Why don't you simply do the following. It's simpler and does the job:

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then 'enter
           If validateIDNO(TextBox1.Text) Then
               'do something
           Else
               MsgBox ("invalid IDNO, please re-enter your idno(focus will go back to textbox now)")
               KeyCode = 0
           End If
       End If
End Sub
0
 

Author Comment

by:MadBeef
ID: 8076733
I tried it but KeyCode is read only.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9053143
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->PAQ - with points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9143677
Question is PAQ'd and points refunded.

YensidMod
Community Support Moderator @Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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