Combobox validation routine in a loop

I found some code that works to validate a combobox in VB.NET 2008.  However, the MessageBox keeps popping up over and over.  It seems that the Enter key I use to close it fires the event over again, so I am in a loop.  How can I turn off the loop?

I also tried PreviewKeyDown, which makes the box appear only twice, which is better, but still not great.

Thanks
If e.KeyCode = Keys.Enter Then
            If CUST_IDComboBox.FindStringExact(CUST_IDComboBox.Text) = -1 Then
                MessageBox.Show("You entered an Invalid Number!")
                Return
            End If
        End If

Open in new window

nordicgirlAsked:
Who is Participating?
 
RobbPConnect With a Mentor Commented:
It will force all validators to run when it is clicked (Whatever the relevant events are), you only want it to validate when the subit action is invoked eg a submit button.

So by setting this to false it should prevent validators to invoke ie you shouldnt get the validator popping up everytime - did you give it a go?
0
 
JackOfPHCommented:
Can you explain us what you want to achieve in your code?

Does this code check the entry in the combobox if it is in the listitem?
0
 
nordicgirlAuthor Commented:
Yes, exactly.  The combobox lists customer numbers.  The users have the option of typing in a number, because there are too many numbers to require the users to scroll down through all of them.  The code checks to make sure the number is a valid number from this list.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
JackOfPHCommented:
No need for this validation just set the AutcompleteMode  property of the combobox to suggest and AutocompleteSource to listitems.
0
 
nordicgirlAuthor Commented:
Well, unfortunately, our managers have may of these numbers memorized and just type them in without looking.  If I type in an invalid number and press enter, there is just no response from the combobox.  That would be warning enough of a typo, but my boss wants an error message.  So therein lies the need. : )
0
 
nordicgirlAuthor Commented:
I got it to work on the Key Down Event.
0
 
RobbPCommented:
Note - make sure the 'CausesValidation' property is set to false on the button in question.
0
 
nordicgirlAuthor Commented:
The default value for 'Causes Validation' for a combobox is True.  What does this property do and why should I set it to False?

Thanks.
0
 
nordicgirlAuthor Commented:
I did, but it didn't seem to make a difference.  In my case, it seems to be related to the focus.  If I click the mouse on the OK button of the message box, it goes away SOMETIMES. It seems to still be focused on the main screen , not the message box.  

Now at home, the keyup event has me pressing the enter key forever, and the keydown event has me doing it 3 times, (2 messasge boxes)  and then the messagebox goes away.  wierd.  

To tell you the truth, I don't know why it started working.( this was earlier today at work). i will have to keep an eye on it.

Thanks.
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.