2017-10-15 108 views
0

我正在为一个学校项目创建一个小型WindowsForm Dice Game,其中一个规范是用障碍物的文本文件读取它们的位置以及可以移回的空间。我已成功将包含此信息的文本文件读入锯齿状数组,现在我需要生成PictureBox来充当障碍物。代码编译良好,一切似乎工作,但PictureBoxes没有显示在我的形式。 x和y值是正确的,并在表单内。 if语句正在检查障碍物是向前还是向后发送玩家并相应地更改图像。在运行时添加PictureBoxes

int obstacleX = Convert.ToInt32(lbl.Location.X) - 14; 
int obstacleY = Convert.ToInt32(lbl.Location.Y) + 6; 
PictureBox obstacle = new PictureBox(); 
if (Library.GlobalVariables.obstacleStats[i][1] < 0) 
{ 
    obstacle.Image = Properties.Resources.badObstacle; 
} 
else 
{ 
    obstacle.Image = Properties.Resources.goodObstacle; 
} 
obstacle.Location = new Point(obstacleX, obstacleY); 
obstacle.Size = new Size(17, 17); 
obstacle.Show(); 
this.Controls.Add(obstacle); 

有什么明显的我失踪了吗?

感谢您的帮助,

乔希

+0

你应该重绘屏幕以显示新的对象。 – nocturns2

+0

如何重画屏幕? this.Refresh()似乎没有任何作用。 –

+0

如果pbox与另一个控件重叠,则追加obstacle.BringToFront()。 –

回答

0

,画面可能无法完全显示,刚刚成立的SizeMode试一试:

obstacle.SizeMode = PictureBoxSizeMode.Zoom 
0

添加obstacle.SizeMode = PictureBoxSizeMode.Zoom解决了这个问题。

下面是对于那些有兴趣的最终代码:

int obstacleX = Convert.ToInt32(lbl.Location.X) - 14; 
int obstacleY = Convert.ToInt32(lbl.Location.Y) + 6; 
PictureBox obstacle = new PictureBox(); 
if (Library.GlobalVariables.obstacleStats[i][1] < 0) 
{ 
    obstacle.Image = Properties.Resources.badObstacle; 
} 
else 
{ 
    obstacle.Image = Properties.Resources.goodObstacle; 
} 
obstacle.Location = new Point(obstacleX, obstacleY); 
obstacle.Size = new Size(17, 17); 
obstacle.SizeMode = PictureBoxSizeMode.Zoom; 
this.Controls.Add(obstacle); 
obstacle.Show(); 
obstacle.BringToFront(); 

感谢您的帮助,

乔希