Validating an item in the Ignore Case list box

There is a problem with a small amount of code designed to add an email alias to the list. I have a built-in check to make sure that the item you are trying to add is not yet on the list, but the check is case sensitive when I don't want it. I'm not sure how to make it ignore the case ... Here is my code:

Dim ItemToAdd as String = ""

ItemtoAdd = tbxItemtoAdd.Text + "@emaildomain.co.uk"

If Not lbxEmailAliases.Items.Contains(ItemtoAdd) Then
    lbxEmailAliases.Items.Add(ItemtoAdd)
End If

Currently, if the list contains johnsmith24 @ emaildomain.co.uk and you are trying to add Johnsmith24 (capital J), it will add it successfully, but I do not want it to be done. How to make him ignore the case?

I tried to change lbxEmailAliases.Items.Contains(ItemtoAdd)to lbxEmailAliases.Items.Contains(ItemtoAdd, StringComparison.CurrentCultureIgnoreCase), but he doesn’t like it, because there are too many arguments, it only takes one.

Any ideas please?

+4
source share
3 answers

If this is a standard WinForm control ListBox, then there is no way to do this without going through all the elements and checking each one separately. For instance:

Dim found As Boolean = False
For Each item As Object In ListBox1.Items
    found = item.ToString().Equals(ItemToAdd, StringComparison.CurrentCultureIgnoreCase)
    If found Then
        Exit For
    End If
Next
If found Then
    lbxEmailAliases.Items.Add(ItemtoAdd)
End If

However, if you are comfortable with LINQ, you can do this more briefly:

If ListBox1.Items.OfType(Of String).Any(Function(item) item.Equals(ItemToAdd, StringComparison.CurrentCultureIgnoreCase)) Then
    lbxEmailAliases.Items.Add(ItemtoAdd)
End If

Or, as Andy G noted, the LINQ method is Containseven simpler as it accepts IEqualityComparer, and a spare one that supports string-insensitive string comparisons is provided by the framework:

If ListBox1.Items.OfType(Of String).Contains(ItemToAdd, StringComparer.CurrentCultureIgnoreCase) Then
    lbxEmailAliases.Items.Add(ItemtoAdd)
End If
+5
source

It should be StringComparer, not StringComparison:

lbxEmailAliases.Items.Contains(ItemtoAdd, StringComparer.InvariantCultureIgnoreCase)

IEqualityComparer, .

, ListBox OfType (Of T):

lbxEmailAliases.Items.OfType(Of String).Contains(ItemtoAdd, StringComparer.InvariantCultureIgnoreCase)
+4

An alternative could be FindString-method:

If lbxEmailAliases.FindString(ItemtoAdd) = ListBox.NoMatches Then
    lbxEmailAliases.Items.Add(ItemtoAdd)
End If

Note . This method searches for items starting with the specified string and returns the index of the first item found. So he will find jdoe@domain.comeven if the existing email address jdoe@domain.computer.com.
So maybe this is not the best solution for your specific case.

EDIT:
You can use instead FindStringExact. Like you, you get the case insensitive required, not a partial comparison.

If lbxEmailAliases.FindStringExact(ItemtoAdd) = ListBox.NoMatches Then
    lbxEmailAliases.Items.Add(ItemtoAdd)
End If
+2
source

Source: https://habr.com/ru/post/1690699/


All Articles