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.
4 10482
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
hi friend,
How can i compare the 'text box text' with the 'listbox items'.
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |