locked
Retrieve ValueMember from multi select listbox RRS feed

  • Question

  • I am in the gruelling process of converting a rather large app from VB6 to VB.Net (2010 express). After spending all day Googling for an answer to this, i am still none the wiser. I have a listbox bound to a an object in the Business Layer, which in turn was populated via a datatable. The listbox is set to multiple selection (MultiSimple). The DisplayMember property is bound to a field called "FullName", and the ValueMember to an integer field called CarerID. My objective is to loop through the listbox and retrieve all CarerIDs from the ValueMember property for each seleted item in the list.

    The closest I have got is is this code in the click event of a button as follows,

            With lstCoursesGraduates
                For i as integer = 0 to lstCoursesGraduates.SelectedItems.Count -1
                    MsgBox(.SelectedValue.ToString)
                Next
            End With
    Obviously it doesn't work as it's not really looping, and keeps displaying the first value. Probably just needs one more line. Any assistance would be most appreciated.
    Sunday, November 18, 2012 12:10 PM

Answers

  • You must cast back the itmes in the listbox to the actual type.  Here is an example assuming the bound datatype is class "Test" and the value field to be displayed is called "val".
     
            For Each item As test In ListBox1.SelectedItems
                MessageBox.Show(item.val)
            Next

    --
    Mike
    • Proposed as answer by Paul IshakModerator Sunday, November 18, 2012 4:45 PM
    • Marked as answer by exxy Sunday, November 18, 2012 10:40 PM
    Sunday, November 18, 2012 12:23 PM
  • Mike, Thanks so much for your reply. Based on your tip and with 5 minutes of configuring, I came up with the following which works perfectly.

    With lstCoursesGraduates
    For Each item As cCarers In .SelectedItems
    MessageBox.Show(CStr(item.CarerID))
    Next
    End With
    Tnaks again

    • Marked as answer by exxy Sunday, November 18, 2012 10:37 PM
    Sunday, November 18, 2012 10:36 PM

All replies

  • You must cast back the itmes in the listbox to the actual type.  Here is an example assuming the bound datatype is class "Test" and the value field to be displayed is called "val".
     
            For Each item As test In ListBox1.SelectedItems
                MessageBox.Show(item.val)
            Next

    --
    Mike
    • Proposed as answer by Paul IshakModerator Sunday, November 18, 2012 4:45 PM
    • Marked as answer by exxy Sunday, November 18, 2012 10:40 PM
    Sunday, November 18, 2012 12:23 PM
  • Mike, Thanks so much for your reply. Based on your tip and with 5 minutes of configuring, I came up with the following which works perfectly.

    With lstCoursesGraduates
    For Each item As cCarers In .SelectedItems
    MessageBox.Show(CStr(item.CarerID))
    Next
    End With
    Tnaks again

    • Marked as answer by exxy Sunday, November 18, 2012 10:37 PM
    Sunday, November 18, 2012 10:36 PM