CheckedListBox 中的项目是否有标签?或者类似的东西?我希望能够存储与我正在显示的项目相关联的 ID。
最佳答案
您不需要 Tag 属性。该控件接受任何对象,这意味着您不必只在其中放入字符串。创建一个包含字符串(并覆盖 ToString()
)和您需要的任何其他数据成员的类。
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
CheckedListBox1.Items.Add(New MyListBoxItem() With {.Name = "One", .ExtraData = "extra 1"})
CheckedListBox1.Items.Add(New MyListBoxItem() With {.Name = "Two", .ExtraData = "extra 2"})
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For Each obj As Object In CheckedListBox1.CheckedItems
Dim item As MyListBoxItem = CType(obj, MyListBoxItem)
MessageBox.Show(String.Format("{0}/{1} is checked.", item.Name, item.ExtraData))
Next
End Sub
End Class
Public Class MyListBoxItem
Private _name As String
Private _extraData As String
Public Property Name As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property ExtraData As String
Get
Return _extraData
End Get
Set(ByVal value As String)
_extraData = value
End Set
End Property
Public Overrides Function ToString() As String
Return Name
End Function
End Class
(被覆盖的
ToString()
决定了将在框中显示的内容。)
关于VB.NET CheckedListBox 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2203915/