4

When I add an item to the CheckedListBox list box I also want to store a reference 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 foreach though the items in CheckedListBox I have a reference to the _referenceItem object. However, when I add items like this, CheckedListBox shows up as blank (the list in the GUI itself). So I am trying to find a way to override the item text 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 am a bit new to wrappers. Why exactly did it work after it was wrapped when it did not work when I added the ReferenceItem directly to the CheckedListBox?

2
  • Please don't start your subject lines with "C#" - that's what we have tags for at Stack Overflow. Commented Jul 29, 2011 at 22:48
  • Do you want to replace the checkboxes in the CheckedListBox with some other kind of control, or so you just want to associate each checkbox with a different object? Commented Jul 29, 2011 at 22:51

1 Answer 1

4

The CheckedListBox uses the ToString method of the objects in the list to populate the captions in the box. Rather than extend the CheckedListBox, just create a wrapper class that lets you store both your reference and a caption, and implements a ToString method which returns your caption. Just create one of your wrapper objects, stick the text in it, stick your reference in it, then add the wrapper object to the list box.

Sign up to request clarification or add additional context in comments.

2 Comments

I have not used wrappers so I read up on them some and tried to implement it and it worked. Thank you!
Looking at your update showing how you fixed the problem, I'm not quite sure why doing it my way made a difference. In the context I had used this trick, the items I was storing didn't have a suitable ToString implemented, and it wasn't feasible to add one, so my wrapper had a separate property for the text to return. In your case, if the object already had a suitable ToString, simply adding it to the list box should have worked without any extra effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.