problems using a checkedlistbox control in a form from

Hi experts!

I have this situation:

A form with many controls. One of this contols is a checkedlistbox.

What I need to do, but can`t figure it out how, is to save all the user`s selection from the checkedlistbox in a variable. Then I need to be able to retrieve this user`s selections stored in a variable, read it (somehow) and then get the selected items checked back in the checkedlistbox.

Did I make my point?

It`s something close to getting the selection and then showing the selection again in the checkedlistbox.

Any helpful idea?
Who is Participating?
nepaluzConnect With a Mentor Commented:
You can also set vaues to specific keys (items in the checklist control) in the dictionary (which has already been populated) directly by:

clbDic.Item(0) = True


clbDic.Item(0) = False

Remember, the index here (0) is just the first item.

            Dim clb As New CheckedListBox
            Dim clbDic As New Dictionary(Of Integer, Boolean)

' to get the set values
            For x = 0 To clb.Items.Count - 1
                clbDic.Add(x, clb.GetItemChecked(x))

'to retrieve the values and set them

            Dim Result As Boolean = False
            For x = 0 To clb.Items.Count - 1
                clb.SetItemCheckState(x, CType(clbDic.TryGetValue(x, Result), CheckState))
zandizzaAuthor Commented:
it seems as a good idea, I`ll try it right away!
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

zandizzaAuthor Commented:
how to clear the elements in the dictionary?

I need to do this process over and over againg.

did I explain myself?
to clear entire contents of dictionary


zandizzaAuthor Commented:
I found an easier way to solve it due to the nature of my problem. Though your solution was really helpful because I didn`t know the existence of the Dictionary class.

I`m pretty  sure I`ll use it in the near future!

Would be nice if you shared your solution ......
zandizzaAuthor Commented:
Ok, here it goes my solution!

I had a CheckedListBox, the data collection was formed by the seven days of the week.

I had to be able to capture the user`s selection in a variable.

The variable was a specific column of a row.

When the user clicked on any row of the datagridview the checkedListBox had to show the store information of the previous user`s selection contained in that row.

My problems was how to capture the user`s selection and then how to retrieve it in the CheckedListBox

The solution I found was this.

to get the days selection

For i As Integer = 0 To checkedListBox.CheckedItems.Count - 1
                        c = chkListDias.CheckedItems(i).ToString
                        If c = "Monday" Then
                            Num = Num + 1
                        ElseIf c = "Tuesday" Then
                            Num = Num + 2
                        ElseIf c = "Wednesday" Then
                            Num = Num + 4
                        ElseIf c = "Thursday" Then
                            Num = Num + 8
                        ElseIf c = "Friday" Then
                            Num = Num + 16
                        ElseIf c = "Saturday" Then
                            Num = Num + 32
                        ElseIf c = "Sunday" Then
                            Num = Num + 64
                        End If

clearing the selected items in the checkedListBox
For i = 0 To 6
                        chkListDias.SetItemCheckState(i, CheckState.Unchecked)

when retrieving the information

it makes a logical comparation of bits, using And. If both numbers are correct, then I otherwise 0.

Dim j As Integer = 0
Dim g As Integer = 0

For j = 0 To 6
                g = Math.Pow(2, j)
                If (Num And g) Then
                    chkListDias.SetItemChecked(j, True)
                End If
Thanks for sharing, I am sure someone will find this helpful in good time.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.