我在所有列表框中都有相同对象类型的列表框,我遇到的问题是我不希望在所有列表框中使用相同的ToString()方法显示对象。有办法解决这个问题吗?
目前我正在向列表框中添加字符串然后我使用所选字符串来搜索对象列表以找到正确的字符串,但我根本不喜欢该解决方案。
答案 0 :(得分:2)
假设有一个像这样的员工类:
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
现在,当您填充列表框时,将列表框的DisplayMember和ValueMember设置为两个不同的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"