Minnophous
Minnophous

Reputation: 17

Making a textbox comparison case-insensitive

Beginner here. I currently have a comparison with a textbox and a listbox, but I need the comparison to be case-insensitive. I keep seeing StringComparison.CurrentCultureIgnoreCase but am unable to fit it into this line of code where I thought it belonged:

if (listBox1.Items[q].Equals(textBoxYourAnswer.Text))
{
    //stuff
}

Does it go here? Does it only work if the comparison text is in quotes, or will it work when the system is looking at a listBox?

Upvotes: 1

Views: 549

Answers (1)

PKing
PKing

Reputation: 106

The listbox item is of type object, but the Equals overload that accepts a StringComparison argument is specific to the String type. If your listbox item is actually of type string, cast it to a string before calling Equals.

if(((string)listBox1.Items[q]).Equals(textBoxYourAnswer.Text, StringComparison.CurrentCultureIgnoreCase)

Upvotes: 1

Related Questions