CheckedListBox.CheckedItemCollection Contains

2 posts, 1 answers
  1. Tarık
    Tarık avatar
    5 posts
    Member since:
    Jun 2016

    Posted 27 Mar 2017 Link to this post


    i have some code like this

    For i = 0 To count
        Dim col1 As CheckedListBox.CheckedItemCollection = chk_Sorumlular.CheckedItems
        Dim col2 As CheckedListBox.CheckedItemCollection = chk_Beyannameler.CheckedItems
        If col1.Contains(Me.Izgara.Rows(i).Cells(Me.sutun_sorumlu.Name).Value) And col2.Contains(Me.Izgara.Rows(i).Cells(Me.sutun_Turu.Name).Value) Then
            Dim str As String = Me.Izgara.Rows(i).Cells(Me.sutun_ID.Name).Value.ToString()
            Dim chrArray() As Char = {"|"c}
            If (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(str.Split(chrArray)(1).Trim(), "", False) <> 0) And chk_TahakkukFisi.Checked Then
                Me.Izgara.Rows(i).Cells(Me.sutun_Sec_Tahakkuk.Name).Value = CheckState.Checked
                Me.Izgara.Rows(i).Cells(Me.sutun_Sec_Tahakkuk.Name).Value = CheckState.Unchecked
            End If
            If (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(str.Split(chrArray)(0).Trim(), "", False) <> 0) And chk_Beyanname.Checked Then
                Me.Izgara.Rows(i).Cells(Me.sutun_Sec_Beyanname.Name).Value = CheckState.Checked
                Me.Izgara.Rows(i).Cells(Me.sutun_Sec_Beyanname.Name).Value = CheckState.Unchecked
            End If
            Me.Izgara.Rows(i).Cells(Me.sutun_Sec_Tahakkuk.Name).Value = CheckState.Unchecked
            Me.Izgara.Rows(i).Cells(Me.sutun_Sec_Beyanname.Name).Value = CheckState.Unchecked
        End If


    i can do this with microsoft checkedlistbox component

    but i try do with radcheckedlistbox and get error

    An unhandled exception of type 'System.InvalidCastException' occurred in asd.exe
    Additional information: An object of type 'System.String' could not be thrown to 'Telerik.WinControls.UI.ListViewDataItem'.


    how can i fix this


    thank you


  2. Answer
    Dimitar avatar
    2117 posts

    Posted 28 Mar 2017 Link to this post

    Hello Tarik,

    Which is the exact line where this exception occurs? If it is when you are calling the Contains method here is how you can create your own method that checks if an item with particular text is checked:
    Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim res = ContainsItem(radCheckedListBox1.CheckedItems, "ListViewItem 5")
    End Sub
    Public Function ContainsItem(ByVal items As ListViewCheckedItemCollection, ByVal text As String) As Boolean
        For Each item As ListViewDataItem In items
            If item.Text = text Then
                Return True
            End If
        Next item
        Return False
    End Function

    I hope this will be useful. Let me know if you have additional questions.

    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top