将 64 个图片框分组为图片框数组
Group 64 picture boxes into picturebox array
我使用 GUI 创建了 64 个图片框。现在我想将这些图片框分组到一个图片框数组中,因为我需要它不断更新所述图片框中的图片。
我有以下代码:
private PictureBox[] pictureBoxArray= new PictureBox[64]; //Initialize array to group picture boxes into picture box array
private void Main_Load(object sender, EventArgs e)
{
ConvertGuiPBtoGuiPbArray(ref pictureBoxArray);
}
public static void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
foreach(PictureBox index in pictureBoxArray)
{
//Some code to do the following:
pictureBoxArray[0]=pictureBox1; //this is the name of the pictureBox on the GUI
pictureBoxArray[1]=pictureBox2;
pictureBoxArray[2]=pictureBox3;
.
.
.
pictureBoxArray[63]=pictureBox64;
//
}
我看过Controls.ofType的命令,但我好像不明白。任何建议将不胜感激。我在这里先向您的帮助表示感谢!
此代码将表单的所有图片框添加到 pictureBoxArray
private PictureBox[] pictureBoxArray;
private void Main_Load(object sender, EventArgs e)
{
pictureBoxArray = this.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}
"this"指的是Form,如果要添加其他容器的图片框如groupbox或任何容器只需替换"this"为您容器的名称.
如果您想按降序对它们进行排序,您可以使用 OrderByDescending 而不是 OrderBy.
完整的代码可以这样:
private static PictureBox[] pictureBoxArray;
public static void ConvertGuiPBtoGuiPbArray(ContainerControl container)
{
pictureBoxArray = container.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}
private void Main_Load(object sender, EventArgs e)
{
ConvertGuiPBtoGuiPbArray(this);//Or Whatever container like groupbox
}
您可以使用 LinQ 查询来搜索具有特定名称的控件:
public void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
for (int i = 0; i <= pictureBoxArray.Count() - 1; i++)
{
int count = i;
pictureBoxArray[i] = (PictureBox)this.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();
}
}
你基本上循环遍历从 0 到 63 的索引并搜索名称 "picturebox + (index+1)" 的图片框并将其分配给数组中的这个索引。
如果源图片框未在 this.Controls
集合中排序,这将跳过问题。
鉴于您对组框的评论:在这种情况下,图片框不是 this.Controls
的一部分,您需要将查询替换为
pictureBoxArray[i] = (PictureBox)groupboxName.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();
假设您受困于窗体的设计和 64 个单独的 pictureBoxNN
属性,基本反射可以根据需要填充数组。我不认为 Controls
可以保证顺序正确。
for (int i = 0; i < 64; i++)
{
pictureBoxArray[i] = (PictureBox)GetType().GetProperty("pictureBox" + (i + 1)).GetValue(this);
}
如果它们没有嵌套在其他控件中,而是直接放在您的窗体上,您可以这样做:
PictureBox[] pictureBoxes = this.Controls.OfType<PictureBox>().ToArray();
编辑:
对于嵌套控件,您需要遍历所有控件容器并检查它们是否包含 PictureBox。你可以这样做:
private void Form_Shown(object sender, EventArgs e)
{
List<PictureBox> pictureBoxes = new List<PictureBox>();
GetAllPictureBoxes(ref pictureBoxes, this.Controls);
PictureBox[] pictureBoxesArray = pictureBoxes.OrderBy(pb => pb.Name).ToArray();
}
private void GetAllPictureBoxes(ref List<PictureBox> pictureBoxes, Control.ControlCollection controls)
{
foreach (Control control in controls)
{
if (control.HasChildren)
GetAllPictureBoxes(ref pictureBoxes, control.Controls);
if (control is PictureBox)
pictureBoxes.Add((PictureBox)control);
}
}
好的,一段时间后我开始使用以下方法:
private PictureBox[] pictureBoxArray = new PictureBox[64]; //Initialize array to group picture boxes into picture box array
private void Main_Load(object sender, EventArgs e)
{
ConvertGuiPBtoGuiPbArray(ref pictureBoxArray);
}
public void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
int i = 0;
string NameofPictureBox;
string PictureBoxNumber;
foreach (var item in groupBox_DataLayer.Controls)
{
if (item is PictureBox)
{
NameofPictureBox = ((PictureBox)item).Name;
PictureBoxNumber = Regex.Match(NameofPictureBox, @"\d+").Value; // \d+ is regex for integers only
i = Int32.Parse(PictureBoxNumber); //Convert only number string to int
i = i - 1; //bcs of index of array starts from 0-63, not 1-64
pictureBoxArray[i] = (PictureBox)item; //put PictureBox object in desired position
}
}
}
这对我来说完全有效,我在排序时遇到了问题,但现在一切正常。谢谢大家的帮助!
我使用 GUI 创建了 64 个图片框。现在我想将这些图片框分组到一个图片框数组中,因为我需要它不断更新所述图片框中的图片。
我有以下代码:
private PictureBox[] pictureBoxArray= new PictureBox[64]; //Initialize array to group picture boxes into picture box array
private void Main_Load(object sender, EventArgs e)
{
ConvertGuiPBtoGuiPbArray(ref pictureBoxArray);
}
public static void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
foreach(PictureBox index in pictureBoxArray)
{
//Some code to do the following:
pictureBoxArray[0]=pictureBox1; //this is the name of the pictureBox on the GUI
pictureBoxArray[1]=pictureBox2;
pictureBoxArray[2]=pictureBox3;
.
.
.
pictureBoxArray[63]=pictureBox64;
//
}
我看过Controls.ofType的命令,但我好像不明白。任何建议将不胜感激。我在这里先向您的帮助表示感谢!
此代码将表单的所有图片框添加到 pictureBoxArray
private PictureBox[] pictureBoxArray;
private void Main_Load(object sender, EventArgs e)
{
pictureBoxArray = this.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}
"this"指的是Form,如果要添加其他容器的图片框如groupbox或任何容器只需替换"this"为您容器的名称.
如果您想按降序对它们进行排序,您可以使用 OrderByDescending 而不是 OrderBy.
完整的代码可以这样:
private static PictureBox[] pictureBoxArray;
public static void ConvertGuiPBtoGuiPbArray(ContainerControl container)
{
pictureBoxArray = container.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}
private void Main_Load(object sender, EventArgs e)
{
ConvertGuiPBtoGuiPbArray(this);//Or Whatever container like groupbox
}
您可以使用 LinQ 查询来搜索具有特定名称的控件:
public void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
for (int i = 0; i <= pictureBoxArray.Count() - 1; i++)
{
int count = i;
pictureBoxArray[i] = (PictureBox)this.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();
}
}
你基本上循环遍历从 0 到 63 的索引并搜索名称 "picturebox + (index+1)" 的图片框并将其分配给数组中的这个索引。
如果源图片框未在 this.Controls
集合中排序,这将跳过问题。
鉴于您对组框的评论:在这种情况下,图片框不是 this.Controls
的一部分,您需要将查询替换为
pictureBoxArray[i] = (PictureBox)groupboxName.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();
假设您受困于窗体的设计和 64 个单独的 pictureBoxNN
属性,基本反射可以根据需要填充数组。我不认为 Controls
可以保证顺序正确。
for (int i = 0; i < 64; i++)
{
pictureBoxArray[i] = (PictureBox)GetType().GetProperty("pictureBox" + (i + 1)).GetValue(this);
}
如果它们没有嵌套在其他控件中,而是直接放在您的窗体上,您可以这样做:
PictureBox[] pictureBoxes = this.Controls.OfType<PictureBox>().ToArray();
编辑:
对于嵌套控件,您需要遍历所有控件容器并检查它们是否包含 PictureBox。你可以这样做:
private void Form_Shown(object sender, EventArgs e)
{
List<PictureBox> pictureBoxes = new List<PictureBox>();
GetAllPictureBoxes(ref pictureBoxes, this.Controls);
PictureBox[] pictureBoxesArray = pictureBoxes.OrderBy(pb => pb.Name).ToArray();
}
private void GetAllPictureBoxes(ref List<PictureBox> pictureBoxes, Control.ControlCollection controls)
{
foreach (Control control in controls)
{
if (control.HasChildren)
GetAllPictureBoxes(ref pictureBoxes, control.Controls);
if (control is PictureBox)
pictureBoxes.Add((PictureBox)control);
}
}
好的,一段时间后我开始使用以下方法:
private PictureBox[] pictureBoxArray = new PictureBox[64]; //Initialize array to group picture boxes into picture box array
private void Main_Load(object sender, EventArgs e)
{
ConvertGuiPBtoGuiPbArray(ref pictureBoxArray);
}
public void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
int i = 0;
string NameofPictureBox;
string PictureBoxNumber;
foreach (var item in groupBox_DataLayer.Controls)
{
if (item is PictureBox)
{
NameofPictureBox = ((PictureBox)item).Name;
PictureBoxNumber = Regex.Match(NameofPictureBox, @"\d+").Value; // \d+ is regex for integers only
i = Int32.Parse(PictureBoxNumber); //Convert only number string to int
i = i - 1; //bcs of index of array starts from 0-63, not 1-64
pictureBoxArray[i] = (PictureBox)item; //put PictureBox object in desired position
}
}
}
这对我来说完全有效,我在排序时遇到了问题,但现在一切正常。谢谢大家的帮助!