Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

problems using a checkedlistbox control in a form from vb.net

Posted on 2010-08-16
9
Medium Priority
?
430 Views
Last Modified: 2013-11-27
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?
0
Comment
Question by:zandizza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 33450394


            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))
            Next

'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))
            Next
0
 

Author Comment

by:zandizza
ID: 33450405
it seems as a good idea, I`ll try it right away!
0
 

Author Comment

by:zandizza
ID: 33450487
how to clear the elements in the dictionary?

I need to do this process over and over againg.

did I explain myself?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 17

Expert Comment

by:nepaluz
ID: 33450508
to clear entire contents of dictionary

clbDic.Clear()

0
 
LVL 17

Accepted Solution

by:
nepaluz earned 2000 total points
ID: 33450523
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

or

clbDic.Item(0) = False

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

Author Comment

by:zandizza
ID: 33455458
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!

Thanks
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33455601
Would be nice if you shared your solution ......
0
 

Author Comment

by:zandizza
ID: 33457180
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
                    Next


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



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
            Next
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33457860
Thanks for sharing, I am sure someone will find this helpful in good time.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question