Adding controls to the Panel works the first couple of times, then fritzes out
Here's screenshots:
Here's the code I'm using to load the pictureBoxes to the Panel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WebServiceScanner
{
public partial class MainForm : Form
{
int pictureYPosition = 8;
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LoadImageFromScanner();
}
private void LoadImageFromScanner()
{
Image pic = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");
PictureBox pictureHolder = new PictureBox();
pictureHolder.Image = pic;
pictureHolder.SizeMode = PictureBoxSizeMode.StretchImage;
pictureHolder.Size = 开发者_运维问答new System.Drawing.Size(180, 250);
pictureHolder.Location = new Point(13, pictureYPosition);
panel1.Controls.Add(pictureHolder);
pictureYPosition += 258;
}
}
}
What could be causing the problem? The Panel has Autoscroll set to true, so maybe that's causing the issue?
IMPORTANT EDIT:
The pictures load absolutely FINE, if I don't touch the scrollbar and leave it in it's initial position (topmost). If I scroll down and add pictures it seems it has a different idea of where the point I'm giving it really is.
Any suggestions?
A panel scrolls its content by adjusting the Location property of its child controls when you move the scrollbar. You need to do this yourself when you add a picture. Fix:
pictureHolder.Location = new Point(13, pictureYPosition + panel1.AutoScrollPosition.Y);
Not sure if this will help. Before you add it to panel call panel1.SuppressLayout()
then afterwards call panel1.ResumeLayout(true)
.
Another option is use a FlowLayoutPanel instead of manually incrementing the distance every time.
The solution that worked for me was to supresslayout and resumelayout, the one of Scott.
To substract the autoscroll Y position, didnt worked at all.
精彩评论