Multiple values in one line of Combo or Listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the (usual) problem that, in a combo or listbox, I want to keep a text
and a related key value. For instance the country description and the country
code.
In order to solve this I uss the following approach:

With the following class I create an item

#Region "Create SubClass Item"
Public Class KeyItem
Inherits ListViewItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ShowItemKey As String, ByVal ShowItemText As String)
MyBase.New()
ItemKey = ShowItemKey
ItemText = ShowItemText
Me.Text = ItemText
End Sub
End Class
#End Region

then I add records to the combo ...

cmb_country.Items.Add(New KeyItem("NL", "Netherlands"))

Although the values are correctly stored in the combo, when displaying the
values instead of seeing "Netherlands" I see : "ListViewItem: {Netherlands}"

How can I get rid of that "ListViewItem:{" part?
Is there an alternate solution?

Thanks for your help

tino
 
tino said:
I have the (usual) problem that, in a combo or listbox, I want to keep a
text
and a related key value.

\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)

' Test.
MsgBox(DirectCast(Me.ComboBox1.Items(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///

Alternatively, you can use a bound combobox:

\\\
With Me.ListBox1

' This can be an 'ArrayList' or array too, for example!
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///

BTW: ListViewItem has nothing to do with comboboxes or listboxes, it's
related to listview controls.
 
Hi Tino

try this:

'In your form

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
_ Handles MyBase.Load
cmb_country.Items.Add(New KeyItem("NL", "Netherlands"))

End Sub

Private Sub cmb_country_SelectedIndexChanged(ByVal sender As Object, ByVal e
As_ System.EventArgs) Handles cmb_country.SelectedIndexChanged
MsgBox(DirectCast(cmb_country.SelectedItem, KeyItem).ItemKey)
End Sub


'Your item
#Region "Create SubClass Item"
Public Class KeyItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ItemKey As String, ByVal ItemText As String)
MyBase.New()
Me.ItemKey = ItemKey
Me.ItemText = ItemText
End Sub
Public Overrides Function tostring() As String
Return ItemText
End Function
End Class
#End Region

hth Peter
 
Thanks very much

Peter, your solution is excellent.

Herfried, unofrtunately with your solution the combobox did not show the
value but the object name instead.

Maybe it would be interesting if future VB versions would allow for a hidden
key value in list or combo boxes.

regards
 
tino said:
Herfried, unofrtunately with your solution the combobox did not show the
value but the object name instead.

You can easily change the item's 'ToString' implementation for the first
part of my solution. For the second part, you will have to change the
'DisplayMember'.
 
Back
Top