Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
Can you tell me where I went wrong?

TableLayout Panel was added to the panel to enable scrolling, and a PictureBox control of a size of 300X300 was added.
However, when a particular row or higher (row=108), the display of the PictureBox control becomes strange and no longer appears.

Following is my Code
C#
namespace TableLayoutPanelImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private const int MAX_ROW = 110;

        private void Form1_Load(object sender, EventArgs e)
        {
            TableLayoutPanel tlp = new TableLayoutPanel();

            tlp.Dock = DockStyle.Top;
            tlp.AutoSize = true;
            tlp.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            tlp.AutoScroll = false;
            tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            tlp.RowCount = MAX_ROW;
            tlp.Parent = this.panel1;

            for (int i = 0; i < MAX_ROW; i++)
            {          // 
                tlp.Controls.Add(new PictureBox()
                {                  
                    Size = new Size(300, 300),
                    BackColor = Color.DimGray,
                    BorderStyle = BorderStyle.FixedSingle,
                    Parent = tlp
                }
                 , 0, i+1);
              
            }
        }
    }
}


What I have tried:

I checked by changing PictureBox size to 200X200, 100X100, but the same problem occurs in particular rows and above.
Posted

1 solution

You don't add the TableLayoutPanel to the Panel1 Controls array, just assign it as the tlp.Parent. So technically, it isn't going to show at all - and if it did, you don't add any pictures to the PictureBoxes, just assign a background colour.
And given that a TLP is a very slow control if you are adding loads of controls to it I'd suspect that you are getting visual effects while the pictures are being added because it's trying to display them as it goes along and running out of time before you add the next one - if so that'll clear itself up once they are all loaded.

But since AutoScroll is set to false and 108 rows of 300 pixels would need a monitor 32,000 pixels high in order to see the last one I can't help thinking this is AI "generated" code which has never been properly tested or even thought about.

I'd stop and think what exactly you are trying to do here, because there are a lot of things in that code that I'd avoid (starting with using lots of Picture boxes and working out from that!)
 
Share this answer
 
Comments
jc_park 1-Aug-24 8:49am    
Thank you for your reply.
I'm sorry, I didn't tell you.
Panel1 is autoscroll=true
So you're adding tlp to panel 1 so you're automatically adding tlp rows and trying to scroll from panel 1.
so I think that don’t need monitor 32,000 pixel
And the absence of the image in PictureBox is a simple sample code that I wrote to check the correct position in TLP first before loading the actual image.
The point I asked is why there is a problem with the display of PictureBox from row 108 and above of TLP.
Dave Kreskowiak 1-Aug-24 9:31am    
Even then, if you're adding hundreds of controls to a form, your entire design is just wrong. The more controls you add, the worse your rendering performance is going to get.

I have no idea what you're doing, but that that number of controls, you would probably be better served drawing the page yourself since you appear to be doing nothing but using controls to show images and colors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900