Using custom objects for (CheckedListBox) .Items.Add ()

When I add an item to the CheckedListBox list, I also want to keep the link to another object. I tried adding a new instance of this object to the CheckedListBox.

public class CheckedListBoxExtention : CheckedListBox { private ReferenceItem _referenceItem; public ReferenceItem storedItem { get { return _referenceItem; } set { _referenceItem = value; } } public CheckedListBoxExtention(ReferenceItem storedItem) { _referenceItem = storedItem; } } 

This works in that later, when I predict, although the elements in the CheckedListBox I have a reference to the _referenceItem object. However, when I add such items, the CheckedListBox appears empty (list in the GUI itself). So I'm trying to find a way to redefine the text of an element or something like that.


This is the code I used to fix the problem.

  class ReferenceItemWrapper { private ReferenceItem _item; public ReferenceItemWrapper(ReferenceItem item) { _item = item; } public ReferenceItem getItem {get {return _item;}} public override string ToString() { return _item.ToString(); } } 

I'm a little new to wrappers. Why did it work after it was wrapped, when it did not work, when I added the ReferenceItem directly to the CheckedListBox?

+4
source share
1 answer

CheckedListBox uses the ToString method for objects in the list to fill in the labels in the field. Instead of extending the CheckedListBox, simply create a wrapper class that allows you to store both the link and the signature, and implements the ToString method, which returns your signature. Just create one of your wrapper objects, paste text into it, paste a link into it, then add a wrapper to the list box.

+4
source

All Articles