469,646 Members | 1,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Adding items in listbox

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

Nov 20 '07 #1
2 2710
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[i]))
{
MessageBox.Show("sorry, '" + _textToAdd + "' is already
listed...");
return;
}

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

Hope this helps

Frank Werner-Krippendorf
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

Nov 20 '07 #2
On Tue, 20 Nov 2007 04:39:00 -0800, Mahmoud Shaban <Mahmoud
Sh****@discussions.microsoft.comwrote:
>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
Nov 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John R. | last post: by
1 post views Thread by John R. | last post: by
3 posts views Thread by Ryan Taylor | last post: by
3 posts views Thread by Husam | last post: by
6 posts views Thread by Papa.Coen | last post: by
14 posts views Thread by Paul_Madden via DotNetMonster.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.