Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms listbox containing custom controls?

Is it possible to make a listbox that lists a bunch of custom controls? I would assume that you might have to invoke some sort of custom drawing of the child objects, but I do not know how to do that. Can anyone shed some light on this?

like image 365
MarkP Avatar asked Jul 14 '10 16:07

MarkP


3 Answers

A ListBox is not designed to be a container control. Its scrollbar cannot scroll the controls. It is in general something you want to avoid, putting a lot of controls in, say, a Panel whose AutoScroll property is True will make your UI unresponsive. Controls are expensive objects.

Take a look at the ListBox.DrawItem event. You can draw your own item and make it look just the way you want it with the Graphics class methods. And it is very cheap. There's a very good example in the MSDN Library article for the event.

like image 177
Hans Passant Avatar answered Nov 19 '22 20:11

Hans Passant


I've done this before not by using a FlowLayoutPanel, but just a normal Panel with controls Docked to the top. You can add a scroll bar, etc.

This works quite nicely for a few controls. More than a few, and it begins to really slow down. If you have the time, I would look into drawing a fake control in it's place, like in Hans Passant's answer, then when the user clicks one of the items, replace it with a real control that looks exactly the same. When that item loses focus, dispose it and change the underlying list.

like image 3
dlras2 Avatar answered Nov 19 '22 20:11

dlras2


Maybe this is what you're looking for: Flexible List Control

I wrote that article some time back.

like image 1
Faraz Azhar Avatar answered Nov 19 '22 21:11

Faraz Azhar