更改所有图片框的高度 C#
Change height of all Pictureboxes C#
我 运行 在尝试更改项目中所有图片框的大小时遇到问题。
图片框是这样创建的:
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);
}
然后,当单击一个框时,我想更改窗体上所有图片框的高度、宽度、填充和名称。目前,我正在使用以下代码:
picbox.BackColor = Color.FromArgb(0, 0, 110);
picbox.Height = 177;
picbox.Width = 314;
picbox.Padding = new Padding(0);
picbox.Name = "picboxnew";
这是行不通的,有谁知道我怎样才能让它工作?
亲切的问候,
您可以使用 Linq 来完成:
var pictureboxes = this.Controls.OfType<PictureBox>().ToList();
foreach (PictureBox pb in pictureboxes)
{
pb.Height = 500;
pb.Name = "some_name";
//etc
}
我 运行 在尝试更改项目中所有图片框的大小时遇到问题。
图片框是这样创建的:
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);
}
然后,当单击一个框时,我想更改窗体上所有图片框的高度、宽度、填充和名称。目前,我正在使用以下代码:
picbox.BackColor = Color.FromArgb(0, 0, 110);
picbox.Height = 177;
picbox.Width = 314;
picbox.Padding = new Padding(0);
picbox.Name = "picboxnew";
这是行不通的,有谁知道我怎样才能让它工作?
亲切的问候,
您可以使用 Linq 来完成:
var pictureboxes = this.Controls.OfType<PictureBox>().ToList();
foreach (PictureBox pb in pictureboxes)
{
pb.Height = 500;
pb.Name = "some_name";
//etc
}