[英]c# Windows Forms adding pictureBoxes to an Array
我需要將PictureBox's
( pictureBox11
到pictureBox30
)添加到數組中。
因此,與其像這樣添加PictureBox:
PictureBox[] Coins = new PictureBox[20];
Coins[0] = pictureBox11;
...
Coins[19] = pictureBox30;
我寫了這樣的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];
}
}
某個地方可能有一個小愚蠢的錯誤,因為我對另一件事使用相同的循環並且它起作用了,idk,也許我只是盲目的看不見錯誤。
也許是相關的,所以我將包括為數組元素分配的代碼:
for (int i = 0; i < Coins.Length; i++)
{
if (player.Bounds.IntersectsWith(Coins[i].Bounds))
{
Coins[i].Visible = false;
}
}
如果我按照第一個代碼中所示的方式添加它們,那么一切都可以正常工作,但這不是很實用。
為什么我編寫的第二個代碼(for循環)不能為我工作?
有沒有更好的方法將多個圖片框添加到數組?
我猜您正在使這一過程變得更加復雜。 為了更好地理解,我假設當您說我需要將pictureboxes(pictureBox11到pictureBox30)添加到數組中時。 這表示您的表單上有30+個PictureBox
,並且每個PictureBox
使用命名約定,因此每個命名為“ pictureBoxX”,其中“ X”為1,2,3…30,31。 然后,您想在表單上獲得一個(連續的)“ PictureBoxes”組以使其不可見。 我希望這是正確的。
為了簡單地使圖片框不可見,我認為不需要數組。 如果名稱與“ pictureBoxX”形式的字符串匹配,只需循環瀏覽圖片框並使其不可見。 我使用IndexsAreValid
方法來驗證開始和結束索引。 該代碼下面的數組實現中的代碼中也使用它。
使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;
}
}
}
}
如果必須返回一個PictureBox
數組,則下面的代碼應該可以工作。
首先,根據需要獲取一個PictureBox
數組,您需要一個數組來存儲它們。 但是首先,您需要知道制作的尺寸。 從您發布的代碼中看來,您想要獲取圖片框11-30並將它們放在一個數組中。 因此,我們可以從這些數字中獲取大小……即30-11 = 19 +1 =20。這就是您所需要的。 只需創建數組並遍歷所有圖片框,然后獲取pictureBox11-pictureBox30。 完成后,我們可以使用此數組使這些“ PictureBoxes”不可見。
我創建了一個類似於tryParse
的方法IsValidPic
來驗證給定索引(1,2,3..30)是否有效。 如果超出范圍,我將忽略該值。 這樣,您便可以在所需的圖片盒不連續的情況下抓取單個圖片盒。 我使用了幾個按鈕來測試方法。
希望這可以幫助。
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.