PC Review


Reply
Thread Tools Rate Thread

Adding items in listbox

 
 
=?Utf-8?B?TWFobW91ZCBTaGFiYW4=?=
Guest
Posts: n/a
 
      20th Nov 2007
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

 
Reply With Quote
 
 
 
 
Frank Werner-Krippendorf
Guest
Posts: n/a
 
      20th Nov 2007
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
>



 
Reply With Quote
 
bob clegg
Guest
Posts: n/a
 
      22nd Nov 2007
On Tue, 20 Nov 2007 04:39:00 -0800, Mahmoud Shaban <Mahmoud
(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding items to columns in a listbox =?Utf-8?B?RGFuaWVsIEJvbmFsbGFjaw==?= Microsoft Excel Misc 1 7th May 2007 04:45 PM
Editing and adding items to a listbox Sophie Microsoft Access External Data 0 30th May 2006 10:34 PM
adding items to <asp:ListBox> via javascript Ryan Taylor Microsoft ASP .NET 3 26th Jan 2005 12:45 AM
Adding Items to a ListBox-Unique Items Only jpendegraft Microsoft Excel Programming 2 2nd May 2004 02:27 AM
Adding items to ListBox sach Microsoft VC .NET 4 23rd Mar 2004 10:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:33 AM.