Sultanen
Sultanen

Reputation: 3412

Add object to listbox and use different string representations depending on the listbox?

I have a coupple of listboxes with the same object type in all of them, the problem i have is that i dont want the object displayed with the same ToString() method in all of the listboxes. Is there a way to solve this?

At the moment im adding strings to the listboxes and then i use the selected string to search a list of objects for the correct one but i dont like that solution at all.

Upvotes: 0

Views: 5990

Answers (1)

Steve
Steve

Reputation: 216293

Suppose to have a class for Employees like this one:

Public Class Employee
    Public Property ID As Integer
    Public Property FirstText As String
    Public Property SecondText As String
    ' and go on with other properties
    ....
End Class

Now when you populate your listboxes, you set the DisplayMember and ValueMember of your listboxes to two different property of Employee

Dim myList As ArrayList = New ArrayList()
myList.Add(New Employee() With {.ID = 1, .FirstText = "John Doe", .SecondText = "Doe John"})
myList.Add(New Employee() With {.ID = 2, .FirstText = "Mark Ross", .SecondText = "Ross Mark"})

ListBox1.DataSource = myList
ListBox2.DataSource = myList

ListBox1.ValueMember = "ID"
ListBox1.DisplayMember = "FirstText"

ListBox2.ValueMember = "ID"
ListBox2.DisplayMember = "SecondText"

Upvotes: 2

Related Questions