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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeepsangshetty
    New Member
    • Aug 2008
    • 12

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

    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.
  • TDHall
    New Member
    • Jul 2008
    • 6

    #2
    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_TextCh anged(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

    Comment

    • sandeepsangshetty
      New Member
      • Aug 2008
      • 12

      #3
      hi friend,
      How can i compare the 'text box text' with the 'listbox items'.

      Comment

      • cloud255
        Recognized Expert Contributor
        • Jun 2008
        • 427

        #4
        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:

        Code:
        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

        Comment

        • joedeene
          Contributor
          • Jul 2008
          • 579

          #5
          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:

          Code:
          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:

          Code:
          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

          Comment

          Working...