2017-10-15 113 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(); 

感謝您的幫助,

喬希