Click here to Skip to main content
13,145,794 members (50,462 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends

I have written a function that validates comboboxes in my project. The function works fine with comboboxes that are populated at design time. But it will not work when I used it with comboboxes that via database at runtime

I have show the function and the validate event handler for comboboxes

The function:

 Public Function validateCombo(txt As ComboBox) As Boolean
 If Not txt.Items.Contains(txt.Text.ToString) Then
    Return False
    Return True
 End If
End Function

Combo event handler:

 Private Sub cboFund_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cboFund.Validating
        If Not bnformclosing Then
            If cboFund.Text <> String.Empty Then
                If validateCombo(cboFund) Then
                    e.Cancel = False
                    MessageBox.Show("The item: " & cboFund.Text & " is not a valid fund", "Invalid fund", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    e.Cancel = True
                End If
            End If
        End If
    End Sub
Posted 17-Dec-12 7:05am
Updated 17-Dec-12 7:08am
Kschuler 17-Dec-12 13:43pm
What error message do you get? What do you mean by "it will not work"?
noblepaulaziz 17-Dec-12 15:26pm
it does not pass validation test. Which means that the item is not in the list whereas the item is in the list
Kschuler 17-Dec-12 16:24pm
Perhaps instead of checking the Text of the combobox, you should check the SelectedValue?
noblepaulaziz 17-Dec-12 16:57pm
Thanks for the reply. I will try your suggestion and let you know the feedback

Thanks once again
Sergey Alexandrovich Kryukov 17-Dec-12 14:28pm
Bad idea: you are showing modal MessageBox each time you are validating. Use something non-modal, just output some validation results in some control on the same form.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

ComboBox.Items.Contains() method search through the object collection which are bound to the ComboBox. When you add string values, this method can be called by providing a string value as parameter. But when you bind a DataTable, the object type will be DaraRow. You have to provide a DataRow object to ComboBox.Items.Contains() method in this scenario.

Or use ComboBox.FindString() or ComboBox.FindStringExact(). That is the easiest way
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

You Can use the Combobox dropdown list property

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100