How can I dynamically populate a CheckedListBox?

I want to populate a CheckedListBox based on the elements passed to the form constructor (in this case List <int >).

My skeletal code for this:

 foreach (int platypus in listPlatypi) { userFriendlyPlatypusName = ExpandFromPlatypusID(platypus); // I want to store a verbose string in an Item of the CheckedListBox, something like: // Item item = new Item(userFriendlyPlatypusName); // what data type should "Item" be? CheckedListBox1.Add(item); } 
+4
source share
2 answers

The answer depends on what you do outside the skeleton code provided. The important thing is that the information your code needs when it acts in the list items later.

CheckedListBox works just like a ListBox . The displayed text is the result of each .ToString() element.

If the lines work, add the display name text.

If you need more information stored for each element, add an .Add() override to your class and a .Add() full element.

If this is not an option, create a small display shell:

 public class PlatypusDisplayWrapper { public Platypus {get; set;} public override string ToString() { return this.Platypus.Name; } } 
+3
source

All Articles