473,480 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2952
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
23335
by: John R. | last post by:
I want to have a listbox that shows a checkbox and a textbox. I created a user control that has a checkbox and a textbox in it and have been trying to add it to a listbox but I can't get it to...
1
1871
by: John R. | last post by:
How do I create an item for a listbox? I want to do something like this EXCEPT for a listbox: ListViewItem item = new ListViewItem(); item.Text = column.Name; int height = item.Bounds.Height;...
3
2701
by: Ryan Taylor | last post by:
Hello. I have an application where I need the user to be able to add items to a listbox. I've implemented this via javascript. The listbox is an <asp:ListBox>. However, when the user submits the...
3
4327
by: Husam | last post by:
Hi EveryBody: I made project by Vb.Net which consist the following items: 1\ Textbox 2\Button 3\Listbox When you write any thing in the textbox and press the button any text written in...
6
3649
by: Papa.Coen | last post by:
I've just spend a lot of time solving the following problem: I used dotNet 2.0 / Visualstudio 2005 / C# / aspx, enable viewstate is set on all controls. I have 2 listboxes; the left contains items...
6
1486
by: mrmagoo | last post by:
I'm trying to add an object to a listbox. I'm using a DataReader from a resultset from a stored procedure. It works except when there are NULL in any of the rows. Then I get an error: An...
14
18625
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
1
12734
by: bill | last post by:
I need to be able to dynamically add items to a <asp:listbox> (or HTML <select runat=server>) using clientside javascript, and retrieve the items when the page is posted back. I use this code to...
0
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.