Link to home
Start Free TrialLog in
Avatar of nordicgirl
nordicgirl

asked on

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

Avatar of JackOfPH
JackOfPH
Flag of Philippines image

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?
Avatar of nordicgirl
nordicgirl

ASKER

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.
No need for this validation just set the AutcompleteMode  property of the combobox to suggest and AutocompleteSource to listitems.
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. : )
I got it to work on the Key Down Event.
Note - make sure the 'CausesValidation' property is set to false on the button in question.
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.
ASKER CERTIFIED SOLUTION
Avatar of RobbP
RobbP
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.