简体   繁体   English

将控件添加到面板中的工作在前几次中起作用,然后逐渐消失

[英]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: 这是我用来将pictureBoxes加载到面板的代码:

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? 面板将Autoscroll设置为true,是否可能是造成此问题的原因?

IMPORTANT EDIT: 重要编辑:

The pictures load absolutely FINE, if I don't touch the scrollbar and leave it in it's initial position (topmost). 如果我不触摸滚动条并将其保留在初始位置(最上方),则图片将完全加载为FINE。 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. 当您移动滚动条时,面板通过调整其子控件的Location属性来滚动其内容。 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) . 在将其添加到面板之前,先调用panel1.SuppressLayout()然后再调用panel1.ResumeLayout(true)

Another option is use a FlowLayoutPanel instead of manually incrementing the distance every time. 另一种选择是使用FlowLayoutPanel而不是每次手动增加距离。

The solution that worked for me was to supresslayout and resumelayout, the one of Scott. 对我有用的解决方案是斯科特之一的supresslayout和resumelayout。

To substract the autoscroll Y position, didnt worked at all. 要减去自动滚动的Y位置,根本没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM