C#Picturebox不会在form1.cs上显示?

时间:2015-04-08 01:13:06

标签: c# winforms

嘿伙计们我试图使用createMap.cs中的函数在form1上创建图片框。然而,一旦我调用该函数没有在form1上绘制图片框?我做错了什么?

我也尝试过调试它,从我能说的话看起来很好......

所以...为什么没有图片盒?

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();
            }
        }

    }

2 个答案:

答案 0 :(得分:0)

您的代码中发生的事情实际上是继续创建New 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。

答案 1 :(得分: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());

    }

在此。而不是在窗体上使用Update我只返回tile(这是一个图片框)并将其添加到load事件的form1控件。希望它有所帮助