更改所有Pictureboxes的高度C#

时间:2017-06-01 14:27:30

标签: c# height picturebox

我在尝试更改项目中所有PictureBox的大小时遇到​​了问题。

PictureBoxes的创建方式如下:

for (int x = 0; x < 2; x++)
                {
                        string filePath = fileEntries[i];
                        string fileName = Path.GetFileName(filePath);
                        int index = Array.IndexOf(fileEntries, filePath);

                        PictureBox image = new PictureBox();
                        image.Text = filePath;
                        image.Top = (int)y * 185 + 8 + 185;
                        image.Left = (int)x * 325 + 9;
                        image.Name = "picbox";
                        image.Click += picbox_Click;
                        image.Image = Image.FromFile(filePath);
                        image.BackColor = Color.FromArgb(0, 0, 110);
                        image.Height = 181;
                        image.Width = 318;
                        image.Padding = new Padding(2);
                        image.Name = "picbox";
                        this.Controls.Add(image);
                    }

然后,当单击一个框时,我想更改窗体上所有PictureBox的高度,宽度,填充和名称。目前,我使用以下代码:

picbox.BackColor = Color.FromArgb(0, 0, 110);
picbox.Height = 177;
picbox.Width = 314;
picbox.Padding = new Padding(0);
picbox.Name = "picboxnew";

这不起作用,是否有人知道如何使这项工作?

亲切的问候,

1 个答案:

答案 0 :(得分:1)

你可以使用Linq:

var pictureboxes = this.Controls.OfType<PictureBox>().ToList();
foreach (PictureBox pb in pictureboxes)
{
    pb.Height = 500;
    pb.Name = "some_name";
    //etc
}