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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.