2015-04-08 33 views
0

嘿傢伙即時嘗試使用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(); 
      } 
     } 

    } 

回答

0

什麼是你的代碼實際上發生的事情是不斷創造新的Form1上 是因爲在你的代碼

private void Form1_Load(object sender, EventArgs e) 
{ 
    createMap CreateMap = new createMap(); 
    CreateMap.renderMap(); //in this part you call the method from the class CreateMap 
} 

在您的方法RenderMap()中,您有此代碼

Form1 canvas = new Form1(); 

它不斷加載一個新的Form1。

+0

我把它移出了功能,它仍然不顯示圖像?或者我必須通過form1來創建一個構造函數或其他東西? – user3585434

+0

修正了它,謝謝。 – user3585434

+0

@ user3585434 對不起,我無法迴應您的評論,但我很高興你修復它。 –

0

試試這個。

首先在你的createMap類中。刪除Form1 canvas = new Form1(); 用這個替換你的代碼。

class createMap 
{ 

    PictureBox tile = new PictureBox();  
    public PictureBox 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}, 
    }; 



     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);       
        tile.Location = new Point(20, 20); 
        tile.Image = Resources.dirt; 
        // canvas.Controls.Add(tile); 
       } 

       if (mapArray[x, y] == 2) 
       { 
        // canvas.createTile(0, 0, 2);          
        tile.Location = new Point(20, 40); 
        tile.Image = Resources.stone; 
        // canvas.Controls.Add(tile); 



       } 

       //canvas.Update(); 

      } 
     } 
     return tile; 
    } 


} 

並在您的Form1_Load事件中更改爲此。

private void Form1_Load(object sender, EventArgs e) 
    { 
     createMap CreateMap = new createMap(); 
     this.Controls.Add(CreateMap.renderMap()); 

    } 

在這。而不是使用窗體上的更新我只是返回瓷磚(這是一個圖片框),並將其添加到加載事件的form1控件。希望它有幫助

相關問題