-1
if (!lstFieldData.Items.Contains(ItemValue))
        MessageBox.Show(ItemValue + "Item not found.");

Above code is to get the list of items that is not in the list. Now i want to check this with ignoring the case. How do i?

DDave
  • 608
  • 6
  • 17
  • no.. that's list. My question is specific for ListBox. – DDave Jan 27 '15 at 08:08
  • @DDave - The answer is the same. Otherwise, what have you tried/researched? – Sayse Jan 27 '15 at 08:09
  • @Sayse not really. It requires Enumerable object. Isn't it. I tried that too. – DDave Jan 27 '15 at 08:10
  • [`ListBox.Items`](https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection%28v=vs.110%29.aspx) is IEnumerable – Sayse Jan 27 '15 at 08:11
  • This worked for me...thanx all if (!lstFieldData.Items.Cast().ToList().Contains(ItemValue,StringComparer.InvariantCultureIgnoreCase)) – DDave Jan 27 '15 at 08:14
  • You have to cast, a ListBox contains objects, not strings. Use lstFieldData.Items.Cast().Contains(ItemValue, StringComparer.CurrentCultureIgnoreCase) – Hans Passant Jan 27 '15 at 08:15

2 Answers2

1

If your lstFieldData consists of only capital letters or lower letters you can use .ToUpper() or .ToLower().

lstFieldData
    A
    B
    C
    D

    if (!lstFieldData.Items.Contains(ItemValue.ToUpper()))
            MessageBox.Show(ItemValue + "Item not found.");
lstFieldData
a
b
c
d
    if (!lstFieldData.Items.Contains(ItemValue.ToLower()))
            MessageBox.Show(ItemValue + "Item not found.");
President Camacho
  • 1,860
  • 6
  • 27
  • 44
-1
if (strCompare.Equals("testcompare", StringComparison.InvariantCultureIgnoreCase))
{
///
}

try this

Faisal
  • 61
  • 1
  • 12