2016-04-15 77 views
1

我想显示13个图片框,但是,它最后只有最后一个可见。 所以我想知道如果我做错了。显示图片框阵列

以下代码从资源文件夹中获取图像。

var testP = new PictureBox(); 
for (int i = 0; i < 13; i++) 
{ 
    testP.Width = 65;         
    testP.Height = 80; 
    testP.BorderStyle = BorderStyle.None; 
    testP.SizeMode = PictureBoxSizeMode.StretchImage; 
    test[i] = getImage(testP, testPTemp[i]);    
} 

下面的代码试图显示13位图片移动的位置。

这两个代码段应该能够执行该操作。

test = new PictureBox[13];  
for (var i = 0; i < 13; i++) 
{ 
    test[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);  
    test[i].Left = 330;  
    test[i].Top = 500;  
    test[i].Location = new Point(test[i].Location.X + 0 * displayShift, test[i].Location.Y); 
    this.Controls.Add(test[i]); 
} 

这里是的getImage()

private PictureBox getImage(PictureBox pB, string i)    // Get image based on the for loop number (i) 
    { 
     pB.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + i);   // Get the embedded image 
     pB.SizeMode = PictureBoxSizeMode.StretchImage; 
     return pB; 
    } 
+1

你想达到什么目的? – Aybe

+0

@Aybe我试图展示13 pictureBox,但它只显示我最后一个,所以我想知道如果我做错了。顺便说一句,感谢您的回复 – Edwardhk

+0

是的,但你如何显示,水平,垂直等...解释*确切*你需要什么。 – Aybe

回答

1

我敢肯定有所有的PictureBox控件,但他们都在同一位置,以便他们在撒谎彼此上方。这就是为什么只有最后一个对你来说是可见的。

我想你应该用i变量替换0。

test[i].Location = new Point(test[i].Location.X + i * displayShift, test[i].Location.Y); this.Controls.Add(test[i]); 
+0

这是我在调试时犯的一个错误, 谢谢指出! – Edwardhk

1

很难根据您提供的代码来判断确切的问题。一个可能的问题是,当您创建PictureBox es时,只能在for循环之前创建一个实例,然后使用该实例的引用填充该数组。另一种可能性是,当您计算控件的X位置时,您将乘以0,这总是会导致0(意味着所有控件位于位置330)。

下面是代码,将基本上实现你正在尝试,但没有你所有的代码,我不能给你一个更具体的例子。

在类

const int PICTURE_WIDTH = 65; 
const int PICTURE_HEIGHT = 85; 

你内心功能

//Loop through each image 
for(int i = 0; i < testTemp[i].length; i++) 
{ 
    //Create a picture box 
    PictureBox pictureBox = new PictureBox(); 

    pictureBox.BorderStyle = BorderStyle.None; 
    pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; 

    //Load the image date 
    pictureBox.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]); 

    //Set it's size 
    pictureBox.Size = new Size(PICTURE_WIDTH, PICTURE_HEIGHT); 

    //Position the picture at (330,500) with a left offset of how many images we've gone through so far 
    pictureBox.Location = new Point(330 + (i * PICTURE_WIDTH), 500); 

    //Add the picture box to the list of controls 
    this.Controls.Add(pictureBox); 
} 

如果您需要保留的图片框列表,只是在循环之前创建一个新的列表,并添加每个pictureBox到循环内的列表。如果要添加这些PictureBox es的控件/窗口需要向左或向右滚动以查看所有图像,请将AutoScroll属性设置为true

+0

omg ...不能相信你用这样干净的代码解决我的问题! 谢谢你! – Edwardhk