Thanks, John, who gave me the right path, because I had the same desire: that the text color of the element be different for each of the three states of the checkbox.
I came up with this subclass of CheckedListBox. It changes the text of the elements, not the background color. It allows you to set 3 colors by the user during development or in code, of course.
It also fixes the problem that I encountered when I had an error while viewing the control in the designer. I also had to overcome the problem that I think would occur in your solution, where, if an element is selected, the base.OnDrawItem method erases the color selection set in the overridden OnDrawItem method. I did this at the expense of the selected element that no longer has a colored background, deleting the e.State part, which says that it is selected so that in base.OnDrawItem it will not become the selected element. This is normal, although I think, since the user will see a focus rectangle that indicates what is selected.
Hope this can be helpful to others. I did not find much for a cohesive solution (not even just the complete OnDrawItem method) when I looked on the net.
using System; using System.Windows.Forms; using System.Drawing; namespace MyNameSpace {
source share