Listbox and Checkbox list in VB 2008

Discussion in 'Visual Basic ( VB )' started by Bosox, Jun 4, 2009.

  1. Bosox

    Bosox New Member

    Joined:
    Jun 4, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I am hoping someone out there has encountered this and can help. I've spent way too much time and getting no where. Also, if you can't help but know where I could go for asstance, that would be most appreciated.

    I am developing a survey type app that allows the user to check items off in a checkbox list.

    The checkbox list are pulled from a db. When the user saves the selected items, it is saved to a different table in the db.

    I have a hidden list box on the page. When the page loads, it checks the db to see if that user had been there before and pulls up their previous selections.

    My issue is that I can't figure out how to search through the Listbox and compare it to the checkbox list and if they have matching values, set the matched items in the check box list as selected.
    :cryin:
    Would anyone out there be able to help?

    Thanks
    Bo
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Hi Bosox,

    This piece code is your required solution :D :
    Code:
    For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
          CheckedListBox1.SetItemCheckState((CheckedListBox1.Items.IndexOf(ListBox1.SelectedItems.Item(i))), CheckState.Checked)
    Next
    
    Put this inside the ListBox1_SelectedIndexChanged procedure.

    The code is quite self-explanatory, but let me briefly explain it :
    (1) It scans through all the selected items in the listbox.
    (2) For each selected item, it performs a search on the CheckedListBox and retrieves the index of the item.
    (3) It then sets the CheckState of that index to CheckState.Checked. :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice