Filling FlowLayoutPanel with many controls and thumbnails on request

I am trying to create an ImageListBox control that displays a large number of thumbnails, such as the one that uses Picasa.

This is my design:

I have a FlowLayoutPanel that is populated with a lot of UserControl objects, for example 4000. Each UserControl assigned a delegate for the Paint event. When the Paint event is triggered, it checks the memory cache for the thumbnail and, if the image is not in the cache, it retrieves it from disk.

I have two problems that I am trying to solve:

  • WinForms seems to fire a Paint event, even if the UserControl not displayed. In fact, only 10 or so controls are in sight, the rest are not ( FlowLayoutPanel.AutoScroll set to true ). As a result, he tries to extract thumbnails for all the images and takes a lot of time.

  • Adding UserControl objects to the FlowLayoutPanel takes a little longer, about 2-3 seconds. I can live with him, but I wonder if there is a better way to do this than:

     UserControl[] boxes = new UserControl[N]; // populate array panel.SuspendLayout(); panel.Controls.AddRange(boxes); panel.ResumeLayout(); 
+6
c # winforms flowlayoutpanel onpaint
source share
3 answers

To increase the flow rate of FlowLayoutPanel using custom controls, disable layout updates when adding controls.

Just before your loop, call SuspendLayout() , and then at the end of the call to ResumeLayout() . Be sure to use try-finally to ensure that ResumeLayout() executed, even if an exception occurs.

+5
source share

I would not add that many user controls. Rather, I will have a series of data structures that stores information about which sketch to use, positioning, etc. Etc., And then handle the rendering of each sketch.

Of course, you should do only what you need, checking the arsenides of the drawing events in your control and rendering sketches that are in sight and that require rendering.

+3
source share

Yeah! I found something.

When the UserControl is not displayed and receives the Paint event, then e.ClipRectangle.IsEmpty is true!

+1
source share

All Articles