Quote:
Originally Posted by cloud255
Hi there,
TDHall's solution sould work, but we could clarify a bit.
I would call the code on the "OnClicked" event of your button instead of on TextChanged. Simply because you would run the code less (bit faster performance) and if the user is entering a word, lets say "sky-diver" and the word "sky" is already in the list, an error would appear saying that "sky" is already in the list.
to compare a listbox object to a textbox.text:
- foreach (object item in this.listBox1.Items)
-
{
-
if(textbox1.text.equals(item.toString())
-
{
-
//show error message; break
-
}
-
}
in addition to this you would want to use a boolean to record if the text is unique, if so, add it to the listbox.
good luck
although this method does work, it is not case-sensitive, example:
if you typed in "hey" and "Hey" was in the listbox it would not show an error message, and still add itself, but if you want it that way its fine, but here's an example for case-sensitivitiness for the whole string:
- private void button1_Click(object sender, EventArgs e)
-
{
-
string textboxtext = textBox1.Text;
-
-
foreach (string item in this.listBox1.Items)
-
{
-
if (textboxtext.ToUpper() == item.ToUpper())
-
{
-
-
MessageBox.Show("Item '" + item + "' exists in the listbox already! (Although casing may not match)");
-
-
-
}
-
}
-
-
}
it works but the casing will show whats in the listbox, and they may not match exactly, but you can add another if statement in there to handle if its exactly the same(case sensitive) or not the same(case-sensitive wise)
hope this helps
p.s. cloud225 that was a good way of checking it, but if he wants case sensitivity i thought i'd show him