Question How to make a text box case insensitive when using to search

LukeDXD

New member
Joined
Feb 21, 2016
Messages
2
Programming Experience
Beginner
Hello,

I'm having a problem getting my textbox to search case insensitive

I'm using a textbox to search within a listbox

VB.NET:
Private Sub tbSearch_TextChanged(sender As Object, e As EventArgs) Handles tbSearch.TextChanged
        For Each item In lbStoresF.Items
            If item.contains(tbSearch.Text) Then
                lbStoresF2.Visible = True
                lbStoresF2.Items.Add(item)
            End If
        Next
    End Sub


If there anyway to make it search with case insensitive?
 
String comparison, Case Insensitive

On way of doing it would be using the String.ToLower method
Try replacing
VB.NET:
If item.contains(tbSearch.Text) Then
with
If item.[B]ToLower[/B].contains(tbSearch.Text.[B]ToLower[/B]) Then

You can also use the IndexOf method, which allows you to specify case-insensitivity. It returns -1 if the substring is not found
 
Thanks Snowflake,

It doesn't seem to like the 'With' keeps saying 'Expression Expected'

I don't know if I'm doing something wrong?

Also how would I use the indexof method?

Thanks
 
Thanks Snowflake,

It doesn't seem to like the 'With' keeps saying 'Expression Expected'

I don't know if I'm doing something wrong?

Also how would I use the indexof method?

Thanks
That "with" was not supposed to be part of the code. That's why you should READ what's posted rather than just copy and paste. The idea was that you replace one line "with" another line.

That said, you should definitely use a case-insensitive comparison rather than forcing text to a particular case and then using a case-sensitive comparison. The latter will usually work but there are time that it won't.
Also how would I use the indexof method?

Thanks
It's a very simple method and used very much like Contains. Have you used the Help menu is VS to find the documentation for that method and try to find out for yourself?
 
Back
Top