How to add icons to TreeView control in c# WITHOUT the ImageList control? I think you need tp call the OnPaint event args but no idea how to do it.
-
the OnPaint event is not called by you, the system calls it and if you have an handler you can specify what you want to do in there. – Davide Piras Aug 30 '11 at 21:19
-
1I'm curious. What's wrong with the image list? – Coincoin Aug 30 '11 at 21:22
3 Answers
Yo have to code OwnerDraw control. Unless you have not really serious reasons, avoid this, as this can turn out into very complicated scenarios, if you want to do it in a good way.
Have a look on this example.

- 61,654
- 8
- 86
- 123
-
not so scary, in fact there is so much Owner Drawing everywhere in Windows. It just has to be handled properly :) – Davide Piras Sep 05 '11 at 10:18
You can override OnDrawNode()
and set DrawMode
to TreeViewDrawMode.OwnerDrawAll
. However, you will soon realise that emulating the exact behaviour of the default tree view is almost impossible. There are some state kept privately by the control that you can't access without a lot of fiddling.
For instance, on a standard tree view control, pressing the mouse button over a node without releasing it will show the node as selected before it's been registered as selected by the treeview control. Trying to emulate that using owner drawn nodes is very hard and basically requires you to reimplement the whole thing.

- 27,880
- 7
- 55
- 76
there are many example on how to do TreeView's nodes owner drawing:

- 1
- 1

- 43,984
- 10
- 98
- 147