|
-
Aug 7th, 2009, 04:51 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Checking the contents of a combobox?
Can someone tell me why this code works exactley the opposite of the way I thought it should?
Code:
If cbKeyWords.FindStringExact(tbAddEdit.Text) = True Then
' Save entry to database
else
MsgBox("Keyword Already Exists")
End If
I have a combobox (cbKeyWords). I want to make sure tbAddEdit.Text does not already exist before I save. To me I should be testing for a False result not True. But the above code, goes to the msgbox if findstringexact=false and saves the data if it's true. Am I crazy or am I missing something? Shouldn't FindStringExact return false if it's not found? thanks
Last edited by dkahn; Aug 7th, 2009 at 06:23 PM.
-
Aug 7th, 2009, 06:45 PM
#2
Addicted Member
Re: Checking the contents of a combobox?
The FindExactString function returns an Integer, not a Boolean. It returns the Index of the first occurrence of a specific String within the Combobox.Items collection. More info about the function on MSDN: ComboBox.FindStringExact Method (String)
If you are looking for a Boolean result for validation, use the Contains function of Combobox.Items collection. [on MSDN: ComboBox.ObjectCollection.Contains Method]
In your case, your code could be changed to:
Code:
If cbKeyWords.Items.Contains(tbAddEdit.Text) = True Then
' Save entry to database
Else
MsgBox("Keyword Already Exists")
End If
Last edited by garyjohn_2000; Aug 7th, 2009 at 06:49 PM.
Anyone who has never made a mistake has never tried anything new. - Einstein
 Peace! 
-
Aug 7th, 2009, 06:46 PM
#3
Re: Checking the contents of a combobox?
FindStringExact doesn't return a boolean. it returns an integer indicating the index it found the string at, which if it didn't find a match would be -1, which when converted to a boolean is true.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2009, 08:23 PM
#4
Thread Starter
Hyperactive Member
Re: Checking the contents of a combobox?
Thanks! I actually tried to use .Contains first but I was getting an error. I was using cbKeyWords.Contains() instead of cbKeyWords.Items.Contains(). Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|