嘿家伙即时尝试使用createMap.cs中的函数在form1上创建图片框。但是,一旦我调用该函数没有pictureboxes正在绘制form1?我做错了什么?C#Picturebox不会显示在form1.cs上?
香港专业教育学院还试图调试它,它似乎从我可以告诉很好..
所以...为什么没有pictureboxes?
Form1.cs中:
private void Form1_Load(object sender, EventArgs e)
{
createMap CreateMap = new createMap();
CreateMap.renderMap();
}
public void createTile(int x, int y, int tile)
{
PictureBox tempTile = new PictureBox();
tempTile.Location = new Point(20, 40);
tempTile.Image = Resources.stone;
Controls.Add(tempTile);
}
createMap.cs:
public void renderMap()
{
int[,] mapArray = new int[10,10]{
{2,2,2,2,2,2,2,2,2,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
{2,1,1,1,1,1,1,1,1,2},
};
Form1 canvas = new Form1();
MessageBox.Show(mapArray.GetLength(0) + ":" + mapArray.GetLength(1));
MessageBox.Show(mapArray[1, 1] + ":" + mapArray[2, 2]);
for(int x = 0; x < mapArray.GetLength(0); x++)
{
for(int y = 0; y < mapArray.GetLength(1); y++)
{
Debug.WriteLine("X:" + x + " Y: " + y + " Tile: " + mapArray[x,y]);
if (mapArray[x, y] == 1)
{
canvas.createTile(0, 0, 1);
PictureBox tile = new PictureBox();
tile.Location = new Point(20, 20);
tile.Image = Resources.dirt;
canvas.Controls.Add(tile);
}
if (mapArray[x, y] == 2)
{
canvas.createTile(0, 0, 2);
PictureBox tile = new PictureBox();
tile.Location = new Point(20, 40);
tile.Image = Resources.stone;
canvas.Controls.Add(tile);
}
canvas.Update();
}
}
}
我把它移出了功能,它仍然不显示图像?或者我必须通过form1来创建一个构造函数或其他东西? – user3585434
修正了它,谢谢。 – user3585434
@ user3585434 对不起,我无法回应您的评论,但我很高兴你修复它。 –