我使用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的命令,但我似乎不理解它。任何建议将不胜感激。我在这里先向您的帮助表示感谢!
答案 0 :(得分:1)
假设您坚持使用表单的设计和64个单独的pictureBoxNN
属性,基本反射可以根据需要填充数组。我认为Controls
不能保证订购正确。
for (int i = 0; i < 64; i++)
{
pictureBoxArray[i] = (PictureBox)GetType().GetProperty("pictureBox" + (i + 1)).GetValue(this);
}
答案 1 :(得分:0)
此代码将表单的所有图片框添加到pictureBoxArray
private PictureBox[] pictureBoxArray;
private void Main_Load(object sender, EventArgs e)
{
pictureBoxArray = this.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}
&#34;如果你想添加另一个容器的图片框,例如 groupbox 或任何容器是指Form >只需替换 &#34;此&#34; ,您的容器名称。
如果您想按降序排序,可以使用 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
}
答案 2 :(得分:0)
如果它们没有嵌套在其他控件中,而是直接放在您的表单上,您可以这样做:
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);
}
}
答案 3 :(得分:0)
好的,所以过了一段时间我开始使用以下方法:
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
}
}
}
这对我来说完全有效,我在排序方面遇到了问题,但现在一切正常。谢谢大家的帮助!
答案 4 :(得分:-1)
您可以使用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的indizes并搜索名称的图片框&#34; picturebox +(index + 1)&#34;并将其分配给数组中的此索引。
如果源图片框未在this.Controls
集合中排序,则会跳过问题。
鉴于您对组合框的评论:在这种情况下,图片框不属于this.Controls
,您需要用
pictureBoxArray[i] = (PictureBox)groupboxName.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();