Connecting Tech Pros Worldwide Forums | Help | Site Map

C# :comparing list box items to the text box text

Newbie
 
Join Date: Aug 2008
Posts: 10
#1: Aug 12 '08
Hi friends,
i created one windows form in c#.
In that form through 'text box' text i want to add items to the 'list box' with the help of button.
Here the problem is 'while entering the items to the list box it should compare the 'ext box text' with the list box items,if the person entering the same item which is in the list box ,it should give an error.
How can i do this please help me.

Newbie
 
Join Date: Jul 2008
Location: Dallas, Texas
Posts: 4
#2: Aug 12 '08

re: C# :comparing list box items to the text box text


Just an idea.

Try firing an event when the user enters a new entry. You can do this using the property window by clicking the events icon (lighting bolt) and selecting the best choice for what you are doing. The "Text Changed" event migt work for you. Then at the:

private void textBox1_TextChanged(object sender, EventArgs e)

method you can loop through the listbox entries using:

foreach (object o in listBox1.Items)
{
MessageBox.Show(o.ToString());
}

Of course, don't use the MessageBox, but your own code to check that the new entry does not match an existing entry in the listbox. Warning: the new entry can only easily be checked for an exact match, if the wording is different or spelling is not the same the check won't find a match.

How this helped.

TH
Newbie
 
Join Date: Aug 2008
Posts: 10
#3: Aug 12 '08

re: C# :comparing list box items to the text box text


hi friend,
How can i compare the 'text box text' with the 'listbox items'.
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#4: Aug 12 '08

re: C# :comparing list box items to the text box text


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:

Expand|Select|Wrap|Line Numbers
  1. foreach (object item in this.listBox1.Items)
  2.             {
  3.                 if(textbox1.text.equals(item.toString())
  4.                {
  5.                     //show error message; break
  6.                 }
  7.             }
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
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#5: Aug 12 '08

re: C# :comparing list box items to the text box text


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:

Expand|Select|Wrap|Line Numbers
  1. foreach (object item in this.listBox1.Items)
  2.             {
  3.                 if(textbox1.text.equals(item.toString())
  4.                {
  5.                     //show error message; break
  6.                 }
  7.             }
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:

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             string textboxtext = textBox1.Text;
  4.  
  5.             foreach (string item in this.listBox1.Items)
  6.             {
  7.                 if (textboxtext.ToUpper() == item.ToUpper())
  8.                 {
  9.  
  10.                     MessageBox.Show("Item '" + item + "' exists in the listbox already! (Although casing may not match)");
  11.  
  12.  
  13.                 }
  14.             }
  15.  
  16.         }

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
Reply