Windows形成CheckedListBox问题

时间:2015-04-16 14:40:48

标签: vb.net winforms checkedlistbox

我正在研究在vb.net中开发的桌面应用程序。我试图根据我从数据库获得的值选择checkedlistbox中的项目。下面是填充checkedlistboxes的代码

Private Sub LoadDisapprovalList()

    cblFedralReasons.Items.Clear()
    cblStateReasons.Items.Clear()

    cblFedralReasons.DataSource = Main.DataClient.DisapprovalReasonList_Get(FedralReason)
    cblFedralReasons.DisplayMember = "DisapprovalReasonTypeDesc"
    cblFedralReasons.ValueMember = "DisapprovalReasonTypeGenId"

    cblStateReasons.DataSource = Main.DataClient.DisapprovalReasonList_Get(StateReason)
    cblStateReasons.DisplayMember = "DisapprovalReasonTypeDesc"
    cblStateReasons.ValueMember = "DisapprovalReasonTypeGenId"

End Sub

之后,我尝试根据数据库中的值选择项目。这是代码

    Private Sub LoadApplicationDisapprovalReasons()
    Dim lstApplicationDisapprovalReasons As New List(Of DataService.usp_ApplicationDisapprovalReason_Get_Result)
    lstApplicationDisapprovalReasons = Main.DataClient.ApplicationDisapprovalReason_Get(_SeqID)

    If lstApplicationDisapprovalReasons.Count > 0 Then

        For Each item In lstApplicationDisapprovalReasons

            Dim selectedDisapprovalId As Integer = item.DisapprovalReasonTypeGenId
            Select Case item.DisapprovalReasonType
                Case FedralReason    
                    Dim selectedIndex = cblFedralReasons.Items.IndexOf(selectedDisapprovalId)
                    cblFedralReasons.SetItemCheckState(selectedIndex, CheckState.Checked)

                Case StateReason

                    Dim selectedIndex = cblStateReasons.Items.IndexOf(selectedDisapprovalId)
                    cblStateReasons.SetItemCheckState(selectedIndex, CheckState.Checked)

            End Select

        Next

    End If

End Sub

但问题是cblFedralReasons.Items.IndexOf总是返回-1。来自数据库的所有数据都是正确的,但有些奇怪的事情发生在checkedlistbox中我无法理解。

编辑: 此外,当我尝试使用以下代码获取项目的文本时,它会返回我的集合的名称而不是文本。

cblFedralReasons.items(1).tostring

返回

DisapprovalReasonList 

而不是该项目的文字!

1 个答案:

答案 0 :(得分:0)

我会试着解释一下我对此的看法:

如果 cblFedralReasons 的数据源为 List(Of DataService.usp_isapprovalReasonList),则搜索 selectedDisapprovalId vía IndexOf 在列表上传递一个Integer ....返回-1值,它是连贯的。
IndexOf ,在集合上,在内部进行 Equals 比较。因此,您要比较不同的类型: Integer DataService.usp_DisapprovalReasonList

有很多方法可以从集合中获取正确的对象。

一个想法可以覆盖你班级中的object.equals:

Public Overrides Function Equals(ByVal p_oAnotherObject As Object) As Boolean 
    If TypeOf p_oAnotherObject Is DataService.usp_DisapprovalReasonList AndAlso Me.GetType.Equals(p_oAnotherObject.GetType) Then
        Return Me.DisapprovalReasonTypeGenId.Equals(DirectCast(p_oAnotherObject, DataService.usp_DisapprovalReasonList).DisapprovalReasonTypeGenId)
    Else
        Return False
    End If
End Function

假设你有一个接受ID的构造函数,你现在可以这样做:

cblFedralReasons.Items.IndexOf(New DataService.usp_DisapprovalReasonList(selectedDisapprovalId)) 

然后,你会得到它。

最后,cblFedralReasons.items(1).tostring,您将获得默认的GetType.Name。在你的课堂上这样做,然后:

Public Overrides Function ToString() As String
    Return DisapprovalReasonTypeDesc
End Function
希望我已经解释过了。