I would like to override the text displayed when an item is added to a checked list box. Right now it is using obj.ToString(), but I want to append some text, without changing the objects ToString method. I have seen examples of handling the DrawItem event for ListBoxs, but when I try to implement them, my event handler is not called. I have noted that the Winforms designer does not seem to allow me to assign a handler for the DrawItem event. Being stubborn, I just added the code myself
listbox1.DrawMode = DrawMode.OwnerDrawVariable;
listbox1.DrawItem += listbox1_DrawItem;
Am I trying to do the impossible?
I continued the work in DonBoitnotts answer.
The "GetCheckBoxState" is implemented using a very limited set with only two states.
Vertically aligned the checkbox and left aligned the text.
Not impossible, but incredibly difficult. What you suggest will not work, note the meta-data in class
CheckedListBox
for methodDrawItem
:Therefore, your only option is to derive your own class from
CheckedListBox
, and in my limited testing, this will be a long road. You can handle the drawing simply enough, as such:Note the method
GetCheckBoxState()
. What you get in theDrawItemEventArgs
is aDrawItemState
, not theCheckBoxState
you need, so you have to translate, and that's where things started to go downhill for me.Soldier on, if you like, this should get you started. But I think it'll be a messy, long road.