views:

41

answers:

3

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?

A: 

This is pretty easy with WPF, just use the basic composition. In a WinForms world, you'll probably need to make the container a custom control.

Greg D
I never actually used WPF, so I'll stick with WinForms. So the container and the items would have to be a custom control.
The container would be, the items might not have to be. Presumably your custom container would contain a collection of `Control` objects.
Greg D
+2  A: 

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.

Hans Passant
A: 

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.

Daniel Rasmussen
you stole my comment and turned it into an answer... brilliant!!!
Luiscencio
@Luiscencio - It was more of an answer in response to your comment, actually. Notice I said I *didn't* use a FlowLayoutPanel, but a simple Panel. The reason for this is if you want to simulate a list box, they're all arranged vertically, and not in the "flow" layout from the FlowLayoutPanel. (Besides, if you wanted your comment to be an answer, you should have posted it as an answer.)
Daniel Rasmussen
I still love you
Luiscencio
@Luiscencio - I'm going to stop trying to detect sarcasm and simply reply with a "Thank you" =]
Daniel Rasmussen