Finding value of win listBox?

  • Thread starter Thread starter VM
  • Start date Start date
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.
 
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
 
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.
 
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).
 
Back
Top