How to set ComboBox's SelectedItem with an object

  • Thread starter Thread starter Sérgio Pinheiro
  • Start date Start date
S

Sérgio Pinheiro

Hi Folks,

I'm populating a combobox, with my own objects using the code bellow...
The id and descriptions are retrieved from a datareader.
Everything works fine, the items population, retrieveing of the current
selected item properties, but.... there is a problem.

How can I set the selecteditem... using this approach.
The form represents a record from one main table; on this table I'm able to
retrieve only the id's used in the comboboxes.
I've tried:
mycombobox.SelectedItem = (New ComboItem(Id, Description))
nothing happens....
mycombobox.Items.IndexOf (New ComboItem(Id, Description))
returns -1

I don't have the Index... without looping thru the items collection (this
kills the performance... because there are too many items).

The other problem is that in real scenario, at the moment I need to set the
selecteditem... I will have only the id (valuemember).

So how can I find the index of the current id or make the right item
selected?

Any suggestions are very wellcome.

thanks in advance...
-------
Id = "10023"
Description = "This is the description"
mycombo.Items.Add(new ComboItem(Id, Description)


Public Class ComboItem
Private _DisplayMember As String
Private _ValueMember As String
Public Sub New(ByVal valorIni As String, ByVal descricaoIni As String)
_ValueMember = valorIni
_DisplayMember = descricaoIni
End Sub
Public Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
End Set
End Property

Public Property ValueMember() As String
Get
Return _ValueMember
End Get
Set(ByVal Value As String)
_ValueMember = Value
End Set
End Property
Public Overrides Function ToString() As String
Return _DisplayMember
End Function
End Class
 
Your problem is that the default comparison for Object uses GetHashValue(),
which in turn is simply a memory address of the object instance.
Consequently, when you are trying to find new ComboboxItem(ID, Description),
a new object is created that is guaranteed *not* to exist in the Combo box
Items collection

You need to override Equals method in your combo item class:

class MyComboBoxItem

{

private string ID;

private string Description;

public MyComboBoxItem(string ID, string Description)

{

this.ID = ID;

this.Description = Description;

}

public override int GetHashCode()

{

return base.GetHashCode ();

}



public override string ToString()

{

return Description;

}

public override bool Equals(object obj)

{

if ( obj is MyComboBoxItem )

return this.ID.Equals((obj as MyComboBoxItem).ID);

return base.Equals (obj);

}

}
 
Thanks again Alex.
It works ...


Alex Feinman said:
Your problem is that the default comparison for Object uses GetHashValue(),
which in turn is simply a memory address of the object instance.
Consequently, when you are trying to find new ComboboxItem(ID, Description),
a new object is created that is guaranteed *not* to exist in the Combo box
Items collection

You need to override Equals method in your combo item class:

class MyComboBoxItem

{

private string ID;

private string Description;

public MyComboBoxItem(string ID, string Description)

{

this.ID = ID;

this.Description = Description;

}

public override int GetHashCode()

{

return base.GetHashCode ();

}



public override string ToString()

{

return Description;

}

public override bool Equals(object obj)

{

if ( obj is MyComboBoxItem )

return this.ID.Equals((obj as MyComboBoxItem).ID);

return base.Equals (obj);

}

}
 
Back
Top