e.cancel= true does not work in textbox validating event if there is a messagebox

Using this code in VB.Net in a textbox validating event, if we do not have the message box it acts as expected.  That is, focus returns to the textbox and waits for further input.  With a message box, the e.cancel seems to be ignored and focus moves to the next control.  We have tried putting the message box both before and after the cancel and get the same results.  What are we doing wrong?

If txtItemNo.Text <> "" Then
            If GetData("ItemNo", txtItemNo.Text, "") <> "" Then
                'do some success stuff
            Else
                MessageBox.Show("Invalid item No!", "Validate Item No", MessageBoxButtons.OK, MessageBoxIcon.Error)
                e.Cancel = True
            End If
 End If
LVL 4
g_johnsonAsked:
Who is Participating?
 
Ryan ChongCommented:
this worked for me:

Private Sub txtItemNo_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles txtItemNo.Validating
        If txtItemNo.Text <> "" Then
            If GetData("ItemNo", txtItemNo.Text, "") <> "" Then
                'do some success stuff
            Else
                MessageBox.Show("Invalid item No!", "Validate Item No", MessageBoxButtons.OK, MessageBoxIcon.Error)
               ' txtItemNo.SelectAll()
                e.Cancel = True
            End If
        End If
    End Sub

Open in new window


or you trying to achieve something else?
0
 
Ryan ChongCommented:
the code above seems "working fine" for me.

what you intend to do in validating event above?
0
 
g_johnsonAuthor Commented:
Do you mean where it says "do some success stuff"?  That just fills in other fields (item description and the like).  If the item exists, focus moves to the next control.  If not, we want to cancel and leave focus where it is.
0
 
g_johnsonAuthor Commented:
No, that's exactly what we are trying to do, but it's not working.  Focus moves to the next control.  I am, however, going to copy your version of it in its entirety to see if we've got a typo somewhere or some such thing.  I'm not sure the signature on the sub routine is the same and will have to check that (it's not my project so somebody else has to check).  Thanks.
0
 
g_johnsonAuthor Commented:
Very strange.  We (coincidentally) rebooted the development machine, and then this worked as originally written.  I can't explain that, but thanks for your input and confirmation that the code should have worked!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.