473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Aug 12 '08 #1
4 10482
TDHall
6
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
Aug 12 '08 #2
hi friend,
How can i compare the 'text box text' with the 'listbox items'.
Aug 12 '08 #3
cloud255
427 Expert 256MB
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
Aug 12 '08 #4
joedeene
583 512MB
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
Aug 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
3
by: Stephen Adam | last post by:
Hi there, I'm sure i'm missing something really simple here, all i want to do is get the value of the selected item in a list box. Even after much fiddling about last night I still could not get...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
4
by: John Wildes | last post by:
Hello I have a small program that I've created to generate accounting statements out of our policy managment system. The first part of the process is selecting the customer to create the...
10
by: Hank1234 | last post by:
I am having a problem with a list box on a form. I have a list box that when I add items to it the text does not show up in the list box. I know thy are added because the side scroll bar changes...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
5
by: =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?= | last post by:
Hello. I am reading a value from a table and trying to determine if that value exists in a list of values associated with a dropdownlist. If so, I select the value, otherwise, I don't. I haven't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.