Edison
Edison

Reputation: 4291

Not being able to add large number of controls in a panel

I have a scenario in which I want to display numerous images in a panel using only horizontal scroll.

To enable only Horizontal Scroll only, I used the following code in the constructor

 public Form()
        {
            InitializeComponent();
            
            panelImageGallery.AutoScroll = true;
            panelImageGallery.VerticalScroll.Enabled = false;
            panelImageGallery.VerticalScroll.Visible = false;


        }

And then to display and use images in the panel I wrote the following lines of code

            int increment = 0;
            lblCount.Text = "0/" + files.Length;

            for (int i=0;i<files.Length;i++)
            {
                PanelPictureBox box = new PanelPictureBox();
                box.IMAGE= files[i];
                box.Location = new Point(panelImageGallery.Location.X + increment, panelImageGallery.Location.Y+10);

                box.INDEX = i+1;
                panelImageGallery.Controls.Add(box);
                increment += 300;

            }

Following is the result, and it can be seen that although, I have 350 images but NOT all images are in the panel as there are only 109 images and even both the horizontal and vertical scrolls are there.

Panel Gallery Images

Also, when I scrolled the panel all the way to the end then there are some display issues at the end too as the last image gets joined with the second last image. The last image

Another thing that I observed was that when I increased the margin between the images, then fewer and fewer images got displayed inside the panel. So for example, when I set the increment to 500 then only 66 images got displayed in the panel instead of all the 350 images. My feeling is that there could be restriction on the maximum size of the panel, So what is actually happening here and how to resolve this issue ?

Upvotes: 1

Views: 119

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125342

This is a limitation because of some of the Windows messages, for example as mentioned in documentations:

Note that the WM_HSCROLL message carries only 16 bits of scroll box position data. Thus, applications that rely solely on WM_HSCROLL (and WM_VSCROLL) for scroll position data have a practical maximum position value of 65,535.

In general, it's not a good idea to try to host too many controls on the form.

In your case, you may want to try controls which performs automatic layout, like FlowLayoutPanel, TableLayoutPanel, Docking into the left of a panel or decrease the margin between your controls.

Or as a proper fix, you can rely on ListView control which support virtual mode, or implement a custom drawn control, or use paging to show a limited number of controls in each page.

Upvotes: 2

Related Questions