473,597 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ 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 2965
Hello Mahmoud,

try this simple function

private void addListboxUniqe Item(string _textToAdd)
{

for (int i = 0; i < this.listBox1.I tems.Count; i++)
{
if (_textToAdd.Equ als(this.listBo x1.Items[i]))
{
MessageBox.Show ("sorry, '" + _textToAdd + "' is already
listed...");
return;
}

}
this.listBox1.I tems.Add(_textT oAdd);
}

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****@discussi ons.microsoft.c omwrote:
>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.ne t 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 ContainsKeymeth od 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
23349
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 show up for each item I add to the listbox. foreach (Column column in columns) { ColumnsListBox.Items.Add(column.Name); ColumnsListBox.Controls.Add(new ColumnListControl(true, column.Name,
1
1890
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; listView1.Items.Add(item); I want to use the item's properties for something else.
3
2715
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 form those items do not get posted. My listbox.Items.Count always returns 0. I want the user to be able to add items without having to resort to a postback, hence the javascript. But I need to be able to access those items in the code behind...
3
4336
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 the textbox will be added to the listbox, but i figure that when
6
3654
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 that can be (dynamically) added to the right box. Adding the first item went fine, it shows up in the right box. Adding the second item resulted in losing _every_ control following the right box. 'View source' reveiled that the rendering of the...
6
1508
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 unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll Additional information: Cast from type 'DBNull' to type 'String' is not valid.
14
18687
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 is that as more and more are added, population of the list box takes longer and longer. ie the first 10th of the item set are added much much quicker than the last 10th. THis occurs with about 40,000 listbox items. My worry is the listbox may...
1
12766
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 add the option to the listbox. It works for <select> or <asp:listbox> controls. var opt = document.CreateElement("OPTION"); opt.text = "test"; opt.value="test";
0
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8381
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6706
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3893
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2409
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 we have to send another system
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.