存储每个ComboBox项的值

时间:2012-05-06 18:38:00

标签: vb.net winforms

由于缺少Value属性,我计划使用类来存储Text项的ValueComboBox属性。到目前为止,我已经成功了。

这是我的班级:

Public Class clCombobox
Public cname As String
Public cvalue As Integer

    Public Property Display() As String
    Get
        Return Me.cname
    End Get
    Set(ByVal value As String)
        Me.cname = value
    End Set
End Property
Public Property Value() As String
    Get
        Return Me.cvalue
    End Get
    Set(ByVal value As String)
        Me.cvalue = value
    End Set
End Property


Public Sub New(ByVal name As String, ByVal value As String)
    cname = name
    cvalue = value
End Sub

Public Overrides Function ToString() As String
    Return cname
End Function
End Class

数据正在添加到ComboBox,如下所示:

cmbComboxBox.Items.Add(New clCombobox("Text", 1))
到目前为止,这似乎有效。但是我如何获取数据。就像我想要所选CheckBox项的价值一样?

我尝试使用:

CType(cmbCombobox.SelectedItem, clCombobox).Value()

没用。

1 个答案:

答案 0 :(得分:1)

根据the documentation,使用SelectedItem property检索您存储在其中的对象。

检索所需值的代码:

Dim selectedItem as clCombobox = CType(cmbComboBox.SelectedItem, clCombobox)
Dim value As Integer = selectedItem.cvalue