Adding items in listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have a problem in adding new listbox items

i don't need to allow adding multible items with the same textvalue

ex: if current items are:

green

red

blue

if red is tried to be added again then error

i thinked about the possible solution:

1.read the current values in listbox in an array
2.compare the textbox textvalue with the all current items textvalues with
if statement
3.raise message error if the textbox textvalue = any item textvalue not the
selected item

any help in how to achieve that by code

by the way

i'm new to C# ,my experience doesn't exceed one year,and i started
programming with studying VB.net but i was not involved in large projects and
i need to go in the right track
so
first i need some tips helping me go faster in programming in general.

second what is the best way to get stareted in any new topic (C# as a
langage,ASP.net 2.0,AJAX,...................... and other new technology

thanks for everyone
 
Hello Mahmoud,

try this simple function

private void addListboxUniqeItem(string _textToAdd)
{

for (int i = 0; i < this.listBox1.Items.Count; i++)
{
if (_textToAdd.Equals(this.listBox1.Items))
{
MessageBox.Show("sorry, '" + _textToAdd + "' is already
listed...");
return;
}

}
this.listBox1.Items.Add(_textToAdd);
}

Hope this helps

Frank Werner-Krippendorf
 
On Tue, 20 Nov 2007 04:39:00 -0800, Mahmoud Shaban <Mahmoud
i have a problem in adding new listbox items

i don't need to allow adding multible items with the same textvalue

ex: if current items are:

green

red

blue

if red is tried to be added again then error

i thinked about the possible solution:

1.read the current values in listbox in an array
2.compare the textbox textvalue with the all current items textvalues with
if statement
3.raise message error if the textbox textvalue = any item textvalue not the
selected item

any help in how to achieve that by code

by the way

i'm new to C# ,my experience doesn't exceed one year,and i started
programming with studying VB.net but i was not involved in large projects and
i need to go in the right track
so
first i need some tips helping me go faster in programming in general.

second what is the best way to get stareted in any new topic (C# as a
langage,ASP.net 2.0,AJAX,...................... and other new technology

thanks for everyone
Hi,
Another possibility is to add the items to a dictionary first.
You can use the dictionary ContainsKeymethod to check if a value is
already there, if it is not present, add it to the dictionary and to
the listbox. If it is already in the dictionary then advise the user.

This avoids iterating over the list. For a small list it is of no
advantage for a large list there may be savings.
hth
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top