您应该将整个对象添加到 中CheckedListBox
,而不仅仅是Names
.
For Each item In Querynames
CheckedListBox1.Items.Add(item)
Next
然后你应该设置DisplayMember
属性。
CheckedListBox1.DisplayMember = "Names"
所以CheckedListBox
只会显示Names
属性。
您可能想要使用常规类而不是匿名类型,因此您可以将项目转换回以访问该tableKey
属性。
考虑以下示例:
Sub Main
Dim clb = new System.Windows.Forms.CheckedListBox()
Dim f = new System.Windows.Forms.Form()
f.Controls.Add(clb)
Dim items = new Item() { new Item() with {.TableKey = "Foo", .Names = "Foobar"},
new Item() with {.TableKey = "ABC", .Names = "ABCDEFG"}}
For Each item in items
clb.Items.Add(item)
next
clb.DisplayMember = "Names"
f.ShowDialog()
Dim message = String.Join(" and ", clb.CheckedItems.Cast(Of Item).Select(Function(i) i.TableKey))
System.Windows.Forms.MessageBox.Show(message)
End Sub
Class Item
Public Property TableKey As String
Public Property Names As String
End Class
另一种方法是将查询结果存储在 an 中Dictionary
,然后查找Names
<->tableKey
对。