Question

I want to put all the words that DO NOT contain the word "Time" in a combobox.

I tried :

foreach (string stt in LTypes)
{
    //if the stat name does not contains TIME
    //Only then we add it to the combobox.

    if (!stt.Contains("Time"))
    { 
        tcomboBox1.Items.Add(stt);    
    }
}

But the above does not works. By does not work I mean that the words containing "Time" also is being inserted in the combobox.

Where did I go wrong ?

Was it helpful?

Solution

Maybe a casing issue.

Try this:

if (!stt.ToUpperInvariant().Contains("TIME"))

This will work regardless of case.

OTHER TIPS

You can use IndexOf with StringComparision.OrdinalIngoreCase

if (!(stt.IndexOf("Time", StringComparision.OrdinalIngoreCase) >=0))

For what we can see from your code and your issue description, either the match fails (due to different case) or your combobox gets populated over and over.

Try this:

//remember to clear your combobox if it is already populated!
tcomboBox1.Items.Clear(); 
foreach (string stt in LTypes)
{
    //setting string to lower/upper case will let you match any occurence of your word
    if (!stt.ToLowerCase().Contains("time"))
    { 
        tcomboBox1.Items.Add(stt);    
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top