setting selected item of a combo box filled with objects

S

Smokey Grindle

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<t> and
IEquatable<t> with 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!
 
R

rowe_newsgroups

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<t> and
IEquatable<t> with 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
 
S

Smokey Grindle

I dont want to make a custom combo box for every type I have like this...
this was just one example...
 
R

rowe_newsgroups

I dont want to make a custom combo box for every type I have like this...
this was just one example...

Ahh, that puts a different spin on things.

Perhaps you could use an Interface on the types that forces them to
implement an integer ID property? As it would be a single member
interface it shouldn't be very hard to implement (especially since you
seem to be using an ID property already) Then you could use the new
combobox class to handle any of these items (this also means you could
add multiple item types to a single combobox and select them by Id -
as long as they implement the interface).

If you don't want to go the Interface route, I'm not sure what to tell
you. You somehow have to have a way to inform the combobox on which
property to inspect to determine equality.

Thanks,

Seth Rowe
 
A

Armin Zingler

Smokey Grindle said:
I dont want to make a custom combo box for every type I have like
this... this was just one example...

No good news, but... I didn't find another way but writing a loop to find
the item. I thought there must be an "IndexOf(Value as object, c as
IComparer)" method, but there isn't. Neither in the class itself, nor in the
base class, nor in the implemented interfaces, nor in the interfaces
implemented by the Combobox' Item property.

So, what's left is

for each item as MyObject in mycombo.items
if item.id = 30 then
mycombo.selecteditem = item
exit for
end if
next


Armin
 
S

Smokey Grindle

right now all the objects are exposed through interface definitions... so
that part is already handled... for some reason I thought in the past I
implemented an icomparable or IEquatable to do what I wanted to do...
because I could of sworn that when it compares it just used the comparision
interface to compare objects when you say to select an object, but when i
stepped through it, that method was never called...
 
H

Harry Strybos

Smokey Grindle said:
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<t> and IEquatable<t> with 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!
MyCombo.SelectedValue = 30...provided you have set the ValueMember property
to the ID of your object
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top