2015-10-04 70 views
2

我正在嘗試創建一個MainCharacter班,其任務是根據每個通過房間傳遞的參數在「房間」對象內創建一個PictureBox時間加載。我如何讓班級在表格內創建一個PictureBox對象

這裏是MainCharacter類的代碼:對於示例創建內部房間1選手客體

namespace VirtualMuseum 
{ 
    class MainCharacter 
    { 
     string characterName; 
     int characterGender; 
     bool registeredUser; 
     int[] playerPosition; 


     // Character constructor 
     public MainCharacter(string name, int gender, bool registered, int[] location) 
     { 
      characterName = name; 
      characterGender = gender; 
      registeredUser = registered; 
      playerPosition = location; 
     } 

     public void drawCharacter() 
     { 
      PictureBox playerBox = new PictureBox(); 
      playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx; 
      playerBox.Width = 28; 
      playerBox.Height = 32; 
      playerBox.Location = new Point(playerPosition[0], playerPosition[1]); 
      playerBox.Visible = true;  
     } 
    } 
} 

和代碼行

MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition); 

的問題是,沒有PictureBox裏面可見當進入創建玩家對象的特定房間時形成。

-----新動作-----

按照您的指示,使用房類

public Hall() 
    { 
     playerPosition = new int[] { 350, 400 }; 

     InitializeComponent(); 
     //pictureBox2.Parent = pictureBox1; 
     MessageBox.Show(playerPosition.ToString()); 

     MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition); 

     player1.drawCharacter(this); 
} 

內下列代碼和MainCharacter類中下面的代碼:

public void drawCharacter(Form form) 
    { 
     PictureBox playerBox = new PictureBox(); 
     playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx; 
     playerBox.Width = 28; 
     playerBox.Height = 32; 
     playerBox.Location = new Point(playerPosition[0], playerPosition[1]); 

     // Add the pictureBox to the selected form 
     form.Controls.Add(playerBox); 
} 

雖然我在drawCharacter方法中定義了picturebox的大小,但我設法在窗體內繪製了東西,但它看起來像一條非常小的線。

formInstance.Controls.Add(playerBox); 

例如:

+0

的PictureBox(及任何控制)必須被添加到形式,以變得可見。 –

回答

2

選項1
有你的窗體的一個實例,在drawCharacter的末尾添加該代碼

然後在您的形式,當你需要調用此方法使用:

var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition); 
player1.drawCharacter(this); 

1選項
你可以改變你的方法返回一個PictureBox

public PictureBox drawCharacter() 
{ 
    PictureBox playerBox = new PictureBox(); 
    // Set properties ... 

    return playerBox; 
} 

然後在您的形式,當你需要調用此方法使用:

var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition); 
this.Controls.Add(player1.drawCharacter()); 

要點:

  • 作爲在「Ivan Stoev」的評論中提到,所有控件必須添加到Controls集合中。
  • 不要忘記將位置屬性設置爲合適的位置或更好的選項,請使用FlowLayoutPanel並將其添加到PictureBox中。例如,如果你把一個FlowLayoutPabel從(flowLayoutPanel1),你可以使用選項2,用添加圖片框,以這種方式

代碼FlowLayoutPanel的:

var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition); 
this.flowLayoutPanel1.Controls.Add(player1.drawCharacter()); 
+0

我試過第一個和第二個選項,但沒有出現。難道我的Room1類是另一個稱爲Location的類的子類,而後者是Form的子類? –

+0

@OrestesMoressis您是否有任何填充表格的面板或某些容器控件? –

+0

不,我只在Room1 [設計]中插入另一個帶有房間背景的PictureBox對象,而在一個小視頻中插入了Windows媒體播放器COM組件。 –

0

你忘了加上PictureBox到形成。沒有一個表格將被安置在您的PictureBox不會顯示。您的代碼可能需要傳遞一個形式進入drawCharacter()方法和添加類似的方法如下內:

YourFormVariable.Controls.Add(playberBox) 

作爲一個方面說明:你不想繼續在增加PictureBox ES的形式和結束,你想添加一個並不斷操縱它。我只提到這是一個警告,因爲drawCharacter()聽起來像可以在「渲染」類型循環中調用它。

0

問題是您沒有將您的表單實例傳遞給您的類,因此無法對其創建控件。您drawCharacter方法更改爲類似:

class MainCharacter 
    { 
    string characterName; 
    int characterGender; 
    bool registeredUser; 
    int[] playerPosition; 

    // Character constructor 
    public MainCharacter(string name, int gender, bool registered, int[] location) 
    { 
     characterName = name; 
     characterGender = gender; 
     registeredUser = registered; 
     playerPosition = location; 
    } 

    public void drawCharacter(Form form) 
    { 
     PictureBox playerBox = new PictureBox(); 
     playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx; 
     playerBox.Width = 28; 
     playerBox.Height = 32; 
     playerBox.Location = new Point(playerPosition[0], playerPosition[1]); 
     playerBox.Visible = true; 
     form.Controls.Add(playerBox); 
    } 
} 

而且使用它像:

MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition); 

player1.drawCharacter(this); 
相關問題