How to ignore case.

TwoWing

Well-known member
Joined
Jan 6, 2006
Messages
71
Location
Near Portsmouth, England
Programming Experience
Beginner
Hello. I am working on a program that checks to see if the string put in a TextBox is already in a ListBox. If it isn't, then the string is added to the ListBox. But the trouble is that the program will accept a word if the case is different to one spelt the same in the ListBox. How can I write the program so that it doesn't matter what the case is? Please. TW.
 
look into the ToUpper or ToLower methods of the string class.

Explaining how you are currently checking for existing entries in the listbox would help (show some code or explain what you're doing).
 
You could use the FindStringExact or Items.Contains methods if you wanted case-sensitive comparisons. If you want case-insensitive then you'll have to loop through the items in the ListBox yourself, e.g.
VB.NET:
Private Function FindStringExactIgnoreCase(ByVal list As ListBox, ByVal value As String) As Integer
    Dim result As Integer = ListBox.NoMatches

    For index As Integer = 0 To list.Items.Count - 1 Step 1
        If String.Compare(value, list.GetItemText(list(index)), True) = 0 Then
            result = index
            Exit For
        End If
    Next index

    Return result
End Function
 
Compare the uppercase value of one string with the uppercase value of the other string, without actually changing the original strings. For example:

If strbox.ToUpper() = lstBox.ToUpper() Then
' they are both the same regardless of case
 
Back
Top