WinForms list containing user controls?

Is it possible to create a list containing a list of custom controls? I would suggest that you might have to invoke some kind of custom drawing of child objects, but I don't know how to do this. Can anyone shed some light on this?

+4
source share
5 answers

ListBox is not intended for container management. Its scrollbar cannot scroll controls. In general, you want to avoid what you want to avoid by putting a lot of controls in, say, Panel, whose AutoScroll property is True, will make your user interface unresponsive. Controls are expensive items.

Take a look at the ListBox.DrawItem event. You can draw your own element and make it the same as you want using the methods of the Graphics class. And it is very cheap. There is a very good example here in

+6
source

I have done this before, not using FlowLayoutPanel, but only a regular panel with controls. Fixed up. You can add a scroll bar, etc.

This works well for multiple controls. More than a few, and it starts to really slow down. If you have time, I would look at the picture of a fake control in it, for example, in the Hans Passant answer , then when the user clicks on one of the elements, replace it with real control, which looks exactly the same. When this item loses focus, delete it and change the base list.

+2
source

Perhaps this is what you are looking for: Flexible list management

I wrote this article a while ago.

+1
source

This is pretty easy with WPF, just use the main composition. In the WinForms world, you probably need to make the container custom.

0
source
private void OnDrawItem(object sender, DrawItemEventArgs e) { Rectangle rect = e.Bounds; rect.Offset(0, -rect.Top); using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) { Control control = (Control)listBox.Items[e.Index]; control.DrawToBitmap(bitmap, rect); rect = e.Bounds; e.Graphics.DrawImage(bitmap, e.Bounds); } } 
0
source

Source: https://habr.com/ru/post/1315692/


All Articles