Finding value of win listBox?

V

VM

How can I get the selected value of a Windows Listbox? It only has two items
(which I added through the Collection property) and everytime I get the
selectedIndex, it returns -1. If I use selectedvalue, I receive a Null
exception. From what I've read, the Text property can't be used (MS declared
it a bug).

Thanks.
 
N

Nathan Price

Are you sure something is actually selected? selectedIndex returns -1 if
nothing is highlighted, otherwise it would return the index of the selected
item.

Nathan Price
 
V

VM

Can it be used without having to hightlight anything? Would it be possible
to just get the value of the listbox (like a combobox)?

Thanks.
 
M

Morten Wennevik

Hi VM,

A ListBox doesn't have a default value like the ComboBox. It is possible to get the first value of the list if that is what you want (or any other value in the list). ListBox.Items[0] will return the first object in the list. You need to cast it to whatever type it is (usually a string).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top