Pergunta

I was doing a ITP project for school. In this project, i made it so that when i add a word into a listbox, there is a filter which searches for the word in the listbox and if the match is false, adds the word into the list. But this filter is not case insensitive meaning it will add the word audi even though there is a Audi, but because the first letter is upper case, the filter does not detects this. the code for this bit is

private void btnAddWord_Click(object sender, EventArgs e)
    {
        if (this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text) == false)
        {
            //if the textbox is empty
            if (tbxAddWord.Text == "")
            {
                MessageBox.Show("You have entered no value in the textbox.");
                tbxAddWord.Focus();
            }
            //if the number of items in the listbox is greater than 29
            if (lbxUnsortedList.Items.Count > 29)
            {
                MessageBox.Show("You have exceeded the maximum number of values in the list.");
                tbxAddWord.Text = "";
            }

            //if the number of items in the listbox is less than 29
            else
            {

                //add word to the listbox
                this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text);
                //update tbxListBoxCount
                tbxListboxCount.Text = lbxUnsortedList.Items.Count.ToString();
                //onclick, conduct the bubble sort
                bool swapped;
                string temp;
                do
                {
                    swapped = false;
                    for (int i = 0; i < lbxUnsortedList.Items.Count - 1; i++)
                    {
                        int result = lbxUnsortedList.Items[i].ToString().CompareTo(lbxUnsortedList.Items[i + 1]);
                        if (result > 0)
                        {
                            temp = lbxUnsortedList.Items[i].ToString();
                            lbxUnsortedList.Items[i] = lbxUnsortedList.Items[i + 1];
                            lbxUnsortedList.Items[i + 1] = temp;
                            swapped = true;
                        }
                    }
                } while (swapped == true);
                tbxAddWord.Text = "";
            }
        }
        if (this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text) == true)
        {
            MessageBox.Show("The word that you have added is already on the list");
            tbxAddWord.Text = "";
            tbxAddWord.Focus();
        }

    }

I want to know how i can make this case insensitive so that the filter will pickup Audi even though the first letter is uppercase.

Foi útil?

Solução

I would simply suggest you this condition, it's not optimal but it works the way you want :

        bool contains = false;

        for (int i = 0; i < lbxUnsortedList.Items.Count; i++)
        {
            if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower())
            {
                contains = true;
            }

        }

        if (!contains)
        {
            //your code
        }
        else
        {
            MessageBox.Show("The word that you have added is already on the list");
            tbxAddWord.Text = "";
            tbxAddWord.Focus();

        }

Outras dicas

Try this

  public bool checkItemExist(string itemToCheck)
    {
         return lbxUnsortedList.Items.Cast<string>.Where(a=>a.ToLower().Equals(tbxAddWord.Text.ToLower()).Count > 0;
    }

This link should give you what you are looking for. Some part of post says

culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase)

Also as an alternative, you can try to store words in the lower case

this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text.ToLower());

Then search for the string in same way

this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text.ToLower()) == true

But in case you are using 'lbxUnsortedList' for display purpose then using a dictionary with key as normal words and value in lower case may also be helpful.

Also if you allow me to throw something else then use string.IsNullOrEmpty(tbxAddWord.Text) instead of tbxAddWord.Text == "". Also you may want to use tbxAddWord.Text = string.Empty;.

Before you compare the two string values, you have to convert the strings to upper case or lower case.

Loop through the list box, lbxUnsortedList to get the items from the list. And compare each string with input string from TextBox, tbxAddWord.

for (int i = 0; i < lbxUnsortedList.Items.Count; i++)
{
    if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower())
    {
         //Your Code
    }
}

for me it is works like this

var query = _repository.Get(e => e.IsDeleteUser != true, null, "Role");

        if (!string.IsNullOrEmpty(filter.UserName))
        {
            query = query.Where(e => e.UserName.**ToLower()**.Contains(filter.UserName**.ToLower()**)).ToList();
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top