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.
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();
}