• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

msgbox triggered last control object

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
MadBeef
Asked:
MadBeef
1 Solution
 
ChayaCommented:
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
 
MadBeefAuthor Commented:
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
 
MadBeefAuthor Commented:
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.

 
MadBeefAuthor Commented:
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
 
ChayaCommented:
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
 
MadBeefAuthor Commented:
I tried it but KeyCode is read only.
0
 
GPrentice00Commented:
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
 
YensidModCommented:
Question is PAQ'd and points refunded.

YensidMod
Community Support Moderator @Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now