On Jul 16, 9:39 am, "Smokey Grindle" <nos...@nospam.comwrote:
Quote:
Say I have a combo box with the following simple object
>
Public class MyObject
public ID as integer
public Name as string
>
public overrides sub ToString() as string
return name
end sub
end Class
>
now say I have a combo box that I add to the screen and add oh say 20 of
these objects to it (I don't want to data bind in this instance)... now say
I have the ID of 30, how would I tell the combo box to select the object
that has the ID of 30? I thought IEquatable and IComparable would do the job
since it would be a comparision... so implement a IComparable<tand
IEquatable<twith the type integer... to compare to the ID... but nothing
happened... so how would you do this? I just want to say say the selected
item = 30 and have the combo box select the correct item.. thanks!
While this might not be the best way, I would shadow the SelectedItem
property and loop through the Items collection.
Something Like:
Public Class MyCombobox
Inherits ComboBox
Public Shadows Property SelectedItem() As Object
Get
Return MyBase.SelectedItem
End Get
Set(ByVal value As Object)
If IsNumeric(value) Then
For Each o As Object In Me.Items
Try
Dim myObject As MyObject = DirectCast(o,
MyObject)
If myObject.ID = CInt(value) Then
MyBase.SelectedItem = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.SelectedItem = value
End If
End Set
End Property
End Class
Please note I only tested this to see if it would work with
"Me.ComboBox1.SelectedItem = SomeInt", so you should do some
additional testing just in case.
Thanks,
Seth Rowe