简体   繁体   English

c#Windows窗体将pictureBoxes添加到数组

[英]c# Windows Forms adding pictureBoxes to an Array

I need to add PictureBox's ( pictureBox11 to pictureBox30 ) to an array. 我需要将PictureBox'spictureBox11pictureBox30 )添加到数组中。

So instead of adding PictureBox's like this: 因此,与其像这样添加PictureBox:

PictureBox[] Coins = new PictureBox[20];
Coins[0] = pictureBox11;
...
Coins[19] = pictureBox30;

I wrote a for cycle like this (DOESN'T WORK) : 我写了这样的for循环(不做):

    for (int i = 11; i < 31; i++)
    {
        for (int j = 0; j < Coins.Length; j++)
        { 
             Coins[j] = (PictureBox)Controls.Find(
                            "pictureBox" + i.ToString(), true)[0];
        }
    }

There might be a small stupid mistake somewhere because I use the same cycle for another thing and it works, idk, maybe I'm just blind and cant see the mistake. 某个地方可能有一个小愚蠢的错误,因为我对另一件事使用相同的循环并且它起作用了,idk,也许我只是盲目的看不见错误。

Maybe it is relevant, so I will include the code I am assigning for the array elements: 也许是相关的,所以我将包括为数组元素分配的代码:

        for (int i = 0; i < Coins.Length; i++)
        {
            if (player.Bounds.IntersectsWith(Coins[i].Bounds))
            {
                Coins[i].Visible = false;
            }
        }

EVERYTHING works fine if I add them as shown in first code, but it is not very practical. 如果我按照第一个代码中所示的方式添加它们,那么一切都可以正常工作,但这不是很实用。

Why isn't the second code (the for cycle) I wrote working for me? 为什么我编写的第二个代码(for循环)不能为我工作?

Is there a better way to add multiple pictureboxes to an array? 有没有更好的方法将多个图片框添加到数组?

I am guessing you are making this more complicated than it has to be. 我猜您正在使这一过程变得更加复杂。 To try and understand better, I assume that when you say I need to add pictureboxes (pictureBox11 to pictureBox30) to an array. 为了更好地理解,我假设当您说我需要将pictureboxes(pictureBox11到pictureBox30)添加到数组中时。 that you mean there are 30+ PictureBox s on your form and each PictureBox uses a naming convention such that each is named “pictureBoxX” where “X” is 1,2,3…30,31. 这表示您的表单上有30+个PictureBox ,并且每个PictureBox使用命名约定,因此每个命名为“ pictureBoxX”,其中“ X”为1,2,3…30,31。 Then you want to get a (consecutive?) group of “PictureBoxes” on the form to make invisible. 然后,您想在表单上获得一个(连续的)“ PictureBoxes”组以使其不可见。 I hope this is correct. 我希望这是正确的。

To simply make the picture boxes invisible, I do not think an array is needed. 为了简单地使图片框不可见,我认为不需要数组。 Simply loop through the picture boxes and make it invisible if the name matches a string of the form “pictureBoxX “. 如果名称与“ pictureBoxX”形式的字符串匹配,只需循环浏览图片框并使其不可见。 I used IndexsAreValid method to validate the start and end indexes. 我使用IndexsAreValid方法来验证开始和结束索引。 It is also used in the code for an array implementation below this code. 该代码下面的数组实现中的代码中也使用它。

Make PictureBoxes invisible without an array 使PictureBoxes在不使用数组的情况下不可见

private void SetPictureBoxesInvisible(int start, int end) {
  int size = -1;
  string targetString = "";
  if (IndexsAreValid(start, end, out size)) {
    for (int i = start; i < end + 1; i++) {
      try {
        targetString = "pictureBox" + i;
        PictureBox target = (PictureBox)Controls.Find(targetString, true)[0];
        if (target != null) {
          target.Visible = false;
        }
      }
      catch (IndexOutOfRangeException e) {
        return;
      }
    }
  }
}

If you must have a PictureBox array returned, then the code below should work. 如果必须返回一个PictureBox数组,则下面的代码应该可以工作。

First, to get an array of PictureBox s as you want you need an array to store them. 首先,根据需要获取一个PictureBox数组,您需要一个数组来存储它们。 But first you need to know how big to make it. 但是首先,您需要知道制作的尺寸。 From your posted code it appears that you want to get picture boxes 11-30 and put them in an array. 从您发布的代码中看来,您想要获取图片框11-30并将它们放在一个数组中。 So we can get the size from these numbers… ie 30-11=19 +1 = 20. That's about all you need. 因此,我们可以从这些数字中获取大小……即30-11 = 19 +1 =20。这就是您所需要的。 Simply create the array and loop through all the picture boxes and grab pictureBox11-pictureBox30. 只需创建数组并遍历所有图片框,然后获取pictureBox11-pictureBox30。 When done we can use this array to make these `PictureBoxes” invisible. 完成后,我们可以使用此数组使这些“ PictureBoxes”不可见。

I created a method IsValidPic similar to a tryParse to validate if the given index (1,2,3..30) is valid. 我创建了一个类似于tryParse的方法IsValidPic来验证给定索引(1,2,3..30)是否有效。 If it is out of range, I simply ignore that value. 如果超出范围,我将忽略该值。 This gives you the ability to grab an individual picture box in case the desired picture boxes are not contiguous. 这样,您便可以在所需的图片盒不连续的情况下抓取单个图片盒。 I used a few buttons to test the methods. 我使用了几个按钮来测试方法。

Hope this helps. 希望这可以帮助。

private PictureBox[] GetPictureBoxes(int start, int end) {
  int size = - 1;
  if (IndexsAreValid(start, end, out size)) {
    PictureBox curPic = null;
    PictureBox[] allPics = new PictureBox[size];
    int index = 0;
    for (int i = start; i <= end; i++) {
      if (IsValidPic(i, out curPic)) {
        allPics[index] = curPic;
        index++;
      }
    }
    return allPics;
  }
  else {
    return new PictureBox[0];
  }
}

private Boolean IndexsAreValid(int start, int end, out int size) {
  if (start < 1 || end < 1) {
    size = -1;
    return false;
  }
  if (start > end) {
    size = -1;
    return false;
  }
  size = end - start + 1;
  return true;
}

private Boolean IsValidPic(int index, out PictureBox picture) {
  string targetName = "pictureBox" + index;
  try {
    PictureBox target = (PictureBox)Controls.Find(targetName, true)[0];
    if (target != null) {
      picture = target;
      return true;
    }
    picture = null;
    return false;
  }
  catch (IndexOutOfRangeException e) {
    picture = null;
    return false;
  }
}

private void ResetAll() {
  foreach (PictureBox pb in this.Controls.OfType<PictureBox>()) {
    pb.Visible = true;
  }
}

private void button1_Click(object sender, EventArgs e) {
  TurnInvisible(2, 3);
}

private void button3_Click(object sender, EventArgs e) {
  TurnInvisible(11, 30);
}

private void button4_Click(object sender, EventArgs e) {
  TurnInvisible(1,7);
}

private void TurnInvisible(int start, int end) {
  PictureBox[] pictureBoxesToChange = GetPictureBoxes(start, end);
  foreach (PictureBox pb in pictureBoxesToChange) {
    if (pb != null)
      pb.Visible = false;
  }
}

private void button2_Click(object sender, EventArgs e) {
  ResetAll();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM