Tutorial :Selecting an item from a listbox



Question:

I read in a particular site that the code:

 txtBox = lstBox.ItemData(lstBox.ListIndex())  

will provide me with the item that is clicked in the listbox where the ListIndex provides me with the row and ItemData provides me with the data in the row. However on trying to implement it I've noticed that there are no such option for my listbox. Is there anything that must be added to the code to avail such options? I am programming using the c# language


Solution:1

i think you got some vb6 code that you're trying to implement in a .net language ... from the .net help:

In Visual Basic 6.0, the ItemData property for a ComboBox control can be set at design time in the Properties window to associate an Integer with a ComboBox item.

In Visual Basic 2008, the ItemData property no longer exists. The Microsoft.VisualBasic.Compatibility library contains a SetItemData function that can be used to associate an Integer with an item; the GetItemData function can be used to retrieve the item.

best regards, don


Solution:2

Use ListBox.SelectedItem. Note that this returns an Object, so you'll have to cast whatever it is back to the type you require. For example:

var selectedItem = (int)myListBox.SelectedItem;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »