I am populating a ComboBox with a bunch of instances of a custom private class:
Expand|Select|Wrap|Line Numbers
- For Each s as Something In MyListOfSomethings
- MyComboBox.Items.Add(New XYZListItem(s.ID,s.description))
- Next
- MyComboBox.ValueMember = "ID"
- MyComboBox.DisplayMember = "Description"
Expand|Select|Wrap|Line Numbers
- Private Class XYZListItem
- Private _description As String
- Private _id As Integer
- Public Property Description() As String
- Get
- Return _description
- End Get
- Set(ByVal value As String)
- _description = value
- End Set
- End Property
- Public Property ID() As Integer
- Get
- Return _id
- End Get
- Set(ByVal value As Integer)
- _id = value
- End Set
- End Property
- Public Sub New(ByVal theID As Integer, ByVal theDescription As String)
- _id = theID
- _description = theDescription
- End Sub
- End Class
My problem is that I want to set the selected item through the SelectedValue property and I can't seem to do so:
Expand|Select|Wrap|Line Numbers
- MyComboBox.SelectedValue = value 'Value being a valid ID for an item listed in the ComboBox
The SelectedValue property can also be used to select an item in the list control by setting it with the value of the item. If no items in the list control contain the specified value, a System..::.ArgumentOutOfRangeException is thrown....using the SelectedValue to set the selected item should be possible...so, since what I'm doing doesn't work (I'm getting no exception....the SelectedValue just remains a value Nothing) how do you set the selected item without having to:
Loop through the items in the ComboBox(For example...)
Find a reference to the one with the ID matching
And then set the ComboBox SelectedItem to the one I found..
Expand|Select|Wrap|Line Numbers
- Dim item As XYZListItem
- For Each thing As XYZListItem In MyComboBox.Items
- If value = thing.ID Then
- item = thing
- Exit For
- End If
- Next
- MyComboBox.SelectedItem = item
-Frinny