多箱C#

多箱C#,c#,winforms,C#,Winforms,我知道pictureBox一次只能显示一个图像。我正试图为我收藏的每幅图像创建一个图片盒。例如,如果我的列表中有十个图像,那么该方法应该为这些相应的图像创建十个pictureBox,以便每个图像都显示在pictureBox中。我不确定用for循环还是foreach循环更好。每次循环增加变量XCoordinate和YCoordinate时,PictureBox的位置应增加,以便PictureBox不会在表单中彼此重叠。使用此方法的原因是,每次应用程序运行时,集合中的图像数都会发生变化。这就是为什么

我知道pictureBox一次只能显示一个图像。我正试图为我收藏的每幅图像创建一个图片盒。例如,如果我的列表中有十个图像,那么该方法应该为这些相应的图像创建十个pictureBox,以便每个图像都显示在pictureBox中。我不确定用for循环还是foreach循环更好。每次循环增加变量XCoordinate和YCoordinate时,PictureBox的位置应增加,以便PictureBox不会在表单中彼此重叠。使用此方法的原因是,每次应用程序运行时,集合中的图像数都会发生变化。这就是为什么我不手动创建它们。因此,完成后,列表中的所有图片都应显示在picture.Box中。盒子的大小应该是一样的,唯一的区别是表格上的位置和里面的图像制作了多个图片盒。请帮忙,我将不胜感激

这应该是什么样的呢?谢谢你给我的代码,我正在为它工作

public List<Image> returnImagesInList()
{
   return this.images;
}



private void createPictureBoxesForImages()
{

        foreach (Image file in  retrurnImagesInList())
        {
            try
            {
                int XCoordinate = 10;
                XCoordinate++;
                int YCoordinate = 5;
                YCoordinate++;
                PictureBox imageControl = new PictureBox();
                imageControl.Height = 100;
                imageControl.Width = 100;

                imageControl.Visible = true;
                imageControl.Location = new Point(XCoordinate, YCoordinate);
                Controls.Add(imageControl);
                imageControl.Image = file;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
}
public List returnImagesInList()
{
返回此图片;
}
私有void CreatePictureBoxesFrimages()的创建
{
foreach(retrurnImagesInList()中的图像文件)
{
尝试
{
int XCoordinate=10;
XCoordinate++;
int YCoordinate=5;
YCoordinate++;
PictureBox imageControl=新PictureBox();
图像控制。高度=100;
图像控制。宽度=100;
imageControl.Visible=true;
图像控制。位置=新点(XCoordinate,yccoordinate);
控件。添加(图像控件);
Image=file;
}
捕获(例外情况除外)
{
MessageBox.Show(“错误:+ex.Message”);
}
}
}

对于每个picturebox,在x和y坐标中只增加一个,其应大于或等于picturebox的宽度和高度。在迭代中使用相同的值初始化坐标,并返回到开始的位置

您需要将初始化带出侧循环,并给出比宽度更大的增量。当picturebox到达表单的右端时,必须增加y坐标

int XCoordinate = 10;
int YCoordinate = 5;
foreach (Image ile in  retrurnImagesInList())
{
    try
    {   
        PictureBox imageControl = new PictureBox();
        imageControl.Height = 100;
        imageControl.Width = 100;
        XCoordinate += imageControl.Width+2;
        if(XCoordinate  > this.Width - imageControl.Width)
        {
            YCoordinate += imageControl.Height + 2;
            XCoordinate = 10;
        }
        imageControl.Visible = true;
        imageControl.Location = new Point(XCoordinate, YCoordinate);
        Controls.Add(imageControl);
        imageControl.Image = file;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

对于每个picturebox,在x和y坐标中仅增加一个,其应大于或等于picturebox的宽度和高度。在迭代中使用相同的值初始化坐标,并返回到开始的位置

您需要将初始化带出侧循环,并给出比宽度更大的增量。当picturebox到达表单的右端时,必须增加y坐标

int XCoordinate = 10;
int YCoordinate = 5;
foreach (Image ile in  retrurnImagesInList())
{
    try
    {   
        PictureBox imageControl = new PictureBox();
        imageControl.Height = 100;
        imageControl.Width = 100;
        XCoordinate += imageControl.Width+2;
        if(XCoordinate  > this.Width - imageControl.Width)
        {
            YCoordinate += imageControl.Height + 2;
            XCoordinate = 10;
        }
        imageControl.Visible = true;
        imageControl.Location = new Point(XCoordinate, YCoordinate);
        Controls.Add(imageControl);
        imageControl.Image = file;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

非常感谢,但是当表格大小达到时,我该如何告诉它?在表格上的新行开始这是目前的情况。我已经更新了代码,您可能需要进行一些调整,但它为您提供了实现这一点的方向。好的,非常感谢上帝保佑您帮助您的生命救世主。欢迎您,非常感谢您。愿上帝也保佑你,谢谢你这么好和善良…非常感谢你,但是当表格大小达到时,我怎么告诉它呢?在表格上的一行开始,这是它目前的样子。我已经更新了代码,你可能需要做一些调整,但它给了你实现这一点的方向。好的,非常感谢上帝保佑你帮助你的生活不客气,你真好。愿上帝也保佑你,谢谢你这么好和善良…我编辑了你的问题不要删除描述,你解释得很好,这将帮助其他人达成解决方案。我编辑了你的问题不要删除描述,你解释得很好,这将帮助其他人达成解决方案。