Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

Update checked listbox items

I have a checked list box on a windows form that I need to update the items that are checked.   The checkedlistbox is already populated with predefined items.    I need check the values in the list box that have been previously selected by the user.  The previously selected values have been stored in a comma seperated string.  As an example, the CheckListBox has the following items

Value1
Value2
Value3
Value4
Value5

When the user last updated the record, he selected Value1 & Value4.   Those values were stored in a database field as follows: "Value1, Value4".    I now need to first check to make sure Value1 and Value4 still exist in the checkbox list.  If they do exist I need to show them as being checked.  If they don't exist I need to add them to the list and then show them as being checked.

I am hoping that is possible but cant figure out how to do it.  
0
Mike_Stevens
Asked:
Mike_Stevens
  • 2
1 Solution
 
jpaulinoCommented:
Hi,
This is what I understood:

        ' Results from the db
        Dim dbResults As String = "Value3, Value4, Value5, Value6"
        Dim myArray() As String = dbResults.Split(","c)
 
        'Check for the items in the CheckedListBox and add it if doesn't found
        For Each item As String In myArray
            Dim x As Integer = Me.CheckedListBox1.FindStringExact(item.Trim)
            If x > 0 Then
                Me.CheckedListBox1.SetItemChecked(x, True)
            Else
                Me.CheckedListBox1.Items.Add(item.Trim, True)
            End If
        Next

Open in new window

0
 
Mike_StevensAuthor Commented:
That is what i was looking for.  Thanks.
0
 
jpaulinoCommented:
Glad I could help!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now