Is this a bug in with listbox

on a web page add    ListBox1 and Button1

on page load add
ListBox1.Items.Add((New ListItem("A", "1")))
ListBox1.Items.Add((New ListItem("B", "2")))
ListBox1.Items.Add((New ListItem("C", "2")))
ListBox1.Items.Add((New ListItem("D", "2")))

On Button1_click add two lines
Dim X as string

If you select item C or D from    listbox1     then  click on  Button1   as a result    ListBox1.SelectedItem.Text   will return    "B"

Is this a bug in or it is normal?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Obadiah ChristopherDeveloper User InterfaceCommented:
Since the value of B, C, D is 2,

X=ListBox1.SelectedItem.Text will return the text of the first item that was added to the list. In this case it is B. This is expected behavior.
saljasAuthor Commented:
It is expected behavior,   is it a bug?
because The returned selected text is (not) the selected text
saljasAuthor Commented:
in other words, if you have a list of 20,000 items then you never know if the returned selectedtext is the right one or no

Also programmers will not use the listbox because they can not guarantee that the value is unique.

Also it is not logical to add only the items that has no similar values and leave others behind!!!
Obadiah ChristopherDeveloper User InterfaceCommented:
--> It's not a bug. It is how all lists behave in programming languages.(atleast in .Net). Try to create a List object and add these items and then try to retrieve it by keyvalue.
--> It is not logical and bad design to add items that have the same value. If this is coming from a database table, the table must be having a primary key. Add the primary key of the table for the value column.
--> If you still need the column, do something like this.

ListBox1.Items.Add((New ListItem("A~1", "1")))
ListBox1.Items.Add((New ListItem("B~2", "2")))
ListBox1.Items.Add((New ListItem("C~2", "3")))
ListBox1.Items.Add((New ListItem("D~2", "4")))

Dim X as string

Open in new window

--> Programmers use Listbox. They just don't have the need to load 20,000 items or to add items that have the same value.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saljasAuthor Commented:
Thank you Mr. Obadiah Christopher

This line solved the issue,

If this is coming from a database table, the table must be having a primary key. Add the primary key of the table for the value column.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.