VB.NET App: CheckedListBox item not showing up as "Checked"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • painkiller
    New Member
    • Nov 2007
    • 17

    VB.NET App: CheckedListBox item not showing up as "Checked"

    language: vb.net
    environment: windows forms
    .net : v1.1

    i am having a checkedlistbox control that display document category such as text, image, video, audio etc. these values are coming from database. when a user clicks any of the above values and saves the form, the item's "value" is getting saved in database. and when a user logs back in, his/her previously checked value items should be shown as checked.

    Everything is working fine except a checked item is not showing up as checked in the form when i try to do it programmitacall y. i know that the code is actually setting the item's checked property to true because itemCheck event gets fired immediately after my code but it just cant show it as checked in the form. here is my vb.net code

    Code:
    sub showCategory()
        dim dt as new datatable
        dt=getDocumentCategory() ' returns doc category as datatable
        
        Dim catarr As New ArrayList
        catarr = getCheckedCategory(userid) 'array of previously checked items
        catarr.TrimToSize()
    
        Me.SuspendLayout()
        clbCategory.DataSource = dt.DefaultView
        clbCategory.DisplayMember = "category_name"
        clbCategory.ValueMember = "id"
        If companyid > 0 Then
               For i As Integer = 0 To clbCategory.Items.Count - 1
                   Dim row As DataRowView = clbCategory.Items.Item(i)
                   If catarr.IndexOf(row(0)) >= 0 Then
                            clbCategory.SetItemChecked(i, True) '<-- this line is setting the item's checked property to true and itemCheck event is getting fired as it should but windows form is not showing up this item as checked.
                            clbCategory.SetSelected(i, True)
                   End If
               Next
        End If
        Me.ResumeLayout()
    end sub
    do you have any idea as to why form is not showing up checkedlistbox' s item as "checked" in the final output??
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Possibly due to postbacks. HTH.

    Comment

    • CyberSoftHari
      Recognized Expert Contributor
      • Sep 2007
      • 488

      #3
      You have to post code of this (clbCategory.Se tItemChecked(i, True) ) method. we cannot identify what you done in this method!
      Suggestion: Do not call a method inside “for” loop just for check an item.

      Comment

      • painkiller
        New Member
        • Nov 2007
        • 17

        #4
        Originally posted by kenobewan
        Possibly due to postbacks. HTH.
        there is no postback. i am calling this function on form load.

        Comment

        • painkiller
          New Member
          • Nov 2007
          • 17

          #5
          Originally posted by CyberSoftHari
          You have to post code of this (clbCategory.Se tItemChecked(i, True) ) method. we cannot identify what you done in this method!
          Suggestion: Do not call a method inside “for” loop just for check an item.
          SetItemChecked function is not written by me. its there in checkedlistbox class :)

          Comment

          • CyberSoftHari
            Recognized Expert Contributor
            • Sep 2007
            • 488

            #6
            [CODE=vbnet]If catarr.IndexOf( row(0)) >= 0 Then [/CODE] is this validation is true?

            Comment

            • painkiller
              New Member
              • Nov 2007
              • 17

              #7
              never mind.......prob lem solved :)
              i put the whole code in form_paint event instead of form_load and its WORKING but cant tell u why. what do u think??

              thanx for the responses anyway!

              Comment

              Working...