Thursday, May 5, 2011

Selecting an item from a listbox

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

From stackoverflow
  • 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

    Avik : Isn't there any way by which I can get hold of the item's row or value directly because otherwise I would have to assign numbers each time some data is added to the listbox
  • 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;
    

0 comments:

Post a Comment