Click here to Skip to main content
11,647,170 members (68,868 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
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
Edited 17-Dec-12 7:08am
Kschuler at 17-Dec-12 13:43pm
What error message do you get? What do you mean by "it will not work"?
noblepaulaziz at 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 at 17-Dec-12 16:24pm
Perhaps instead of checking the Text of the combobox, you should check the SelectedValue?
noblepaulaziz at 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 at 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
0 500
1 F-ES Sitecore 370
2 DamithSL 335
3 OriginalGriff 322
4 CPallini 290
0 OriginalGriff 1,277
1 994
2 DamithSL 946
3 Sergey Alexandrovich Kryukov 853
4 CPallini 760

Advertise | Privacy | Mobile
Web01 | 2.8.150804.2 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2015
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