Solved

Synchronising an arraylist with a checkedlistbox

Posted on 2010-11-20
4
492 Views
Last Modified: 2012-05-10
I am using an ArrayList in one of my classes to hold a collection of "People" objects. I am also using a CheckedListBox component on a form to present this collection of people on screen.

I have a button which adds a new person (from a set of input TextBoxes) to both the ArrayList and the CheckedListBox at the same time so as to keep them in synch.

My question -
Is there any way I can set the collection in the CheckedListBox to be the same as that in the ArrayList so that the two are automatically kept in synch?
0
Comment
Question by:Jedeye
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34179674
I think you are already using the best approach. There are other more complicated approaches but this one is the simplest
0
 
LVL 13

Expert Comment

by:Corey2
ID: 34181952
If you use a bindinglist instead of an arraylist you can set the bindinglist as the datasource of the checkedlistbox.  Then you can add directly to the bindinglist only and the checkedlistbox should update automatically.  Arraylists won't work the same because they don't raise listChanged events
0
 

Author Comment

by:Jedeye
ID: 34182692
Hi Corey2
Your solution sounds as though it would be perfect - but I can't find any obvious way to add the bindinglist as the datasource of the checkedlistbox - I am probably missiing something - can you enlighten me?
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 34184004
You simply set the datasource property of the Checkedlistbox to your bindinglist. If your bindinglist is filled with a more complex object you may need to use the DisplayMember and ValueMember properties to have more control over the behavior


Dim BL as new bindinglist (of String)
Checkedlistbox1.datasource = bl

bl.add("One Fish")
bl.add ("Two Fish")
bl.add ("Red Fish")
bl.add ("Blue Fish")

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now