我无法刷新/重新打印winform

时间:2011-03-19 16:28:07

标签: c# winforms picturebox

基本上,我做了一个棋盘。 我有2节课。触发事件的partial类,以及执行国际象棋游戏代码的逻辑类(即MVC模型)。

基本上,我现在正在做的是回复游戏。当然,游戏必须重新启动(重绘)本身。

首先,我将向您解释图片的加载方式。

下载国际象棋时的算法。

    PictureBox[,] chessPics = new PictureBox[9, 9];// this is an array of pictureBox. each square is a picture box

    private void Chess_Load(object sender, EventArgs e)
    {        
        chessPics = Pattern();// pattern is a function that returns an array of 
        PrintPieces(codeFile.FirstTimeLoad());// this FirstTimeLoad method, goes to the chess code file an brings a new array of all the chess pieces (each chess piece is an object).        
    }


        public PictureBox[ , ] Pattern()
    {
        chessPics[1, 1] = a1;// each letter and number is an event that triggers the picture.
        chessPics[1, 2] = b1;
        chessPics[1, 3] = c1;
        chessPics[1, 4] = d1;
        chessPics[1, 5] = e1;
        chessPics[1, 6] = f1;
        chessPics[1, 7] = g1;
        chessPics[1, 8] = h1;
        chessPics[2, 1] = a2;
        chessPics[2, 2] = b2;
        chessPics[2, 3] = c2;
        chessPics[2, 4] = d2;
        chessPics[2, 5] = e2;
        chessPics[2, 6] = f2;
        chessPics[2, 7] = g2;
        chessPics[2, 8] = h2;
        chessPics[3, 1] = a3;
        chessPics[3, 2] = b3;
        chessPics[3, 3] = c3;
        chessPics[3, 4] = d3;
        chessPics[3, 5] = e3;
        chessPics[3, 6] = f3;
        chessPics[3, 7] = g3;
        chessPics[3, 8] = h3;
        chessPics[4, 1] = a4;
        chessPics[4, 2] = b4;
        chessPics[4, 3] = c4;
        chessPics[4, 4] = d4;
        chessPics[4, 5] = e4;
        chessPics[4, 6] = f4;
        chessPics[4, 7] = g4;
        chessPics[4, 8] = h44;
        chessPics[5, 1] = a5;
        chessPics[5, 2] = b5;
        chessPics[5, 3] = c5;
        chessPics[5, 4] = d5;
        chessPics[5, 5] = e5;
        chessPics[5, 6] = f5;
        chessPics[5, 7] = g5;
        chessPics[5, 8] = h5;
        chessPics[6, 1] = a6;
        chessPics[6, 2] = b6;
        chessPics[6, 3] = c6;
        chessPics[6, 4] = d6;
        chessPics[6, 5] = e6;
        chessPics[6, 6] = f6;
        chessPics[6, 7] = g6;
        chessPics[6, 8] = h6;
        chessPics[7, 1] = a7;
        chessPics[7, 2] = b7;
        chessPics[7, 3] = c7;
        chessPics[7, 4] = d7;
        chessPics[7, 5] = e7;
        chessPics[7, 6] = f7;
        chessPics[7, 7] = g7;
        chessPics[7, 8] = h7;
        chessPics[8, 1] = a8;
        chessPics[8, 2] = b8;
        chessPics[8, 3] = c8;
        chessPics[8, 4] = d8;
        chessPics[8, 5] = e8;
        chessPics[8, 6] = f8;
        chessPics[8, 7] = g8;
        chessPics[8, 8] = h8;

        return chessPics;

    }

在图片框上打印每张图片的方法:

        public  void PrintPieces(Pieces [,] pieces)
    {      
        for (int i = 1; i < 9; i++)
        {             
            for (int j = 1; j < 9; j++)
            {
                if (pieces[i, j] is Object)
                {
                   chessPics[i, j].Load(pieces[i, j].print());    // in the pieces object there are objects with a print method which specifies from which location to load the pictures             
                }
                else
                {
                    chessPics[i, j].Image = null;
                }
            }             
        }
    }

我未能做到的,是在游戏的中间或结束时将所有内容重置为开始。 我试图创建新的对象和新的方法,将复制我在加载函数形式时所做的算法,但没有任何效果。

有什么解决方案吗?


这是我的Class2:

   public class Class2
 {
    public static bool MATE;
    Pieces[,] pieces;
    Pieces[,] piece;

我创建了一个新对象

        Class2 codeFileReplay = new Class2();

甚至分配了一种重置和撤退新阵列的方法:

     public void resetBoard()
    {
        piece = null;
        pieces = null;
    }

我激活了那个方法,并再次完成了整个过程,将新对象的新数组发送到打印板的方法

        public static Pieces[,] ChessBoardDisplay()
    {
        Pieces[,] piece2 = new Pieces[9, 9];

        piece2[8, 1] = new Rook("WR");
        piece2[8, 2] = new Knight("WKN");
        piece2[8, 3] = new Bishop("WB");
        piece2[8, 5] = new Queen("WQ");
        piece2[8, 4] = new King("WK");
        piece2[8, 6] = new Bishop("WB");
        piece2[8, 7] = new Knight("WKN");
        piece2[8, 8] = new Rook("WR");


        piece2[7, 1] = new Pawn("WP");
        piece2[7, 2] = new Pawn("WP");
        piece2[7, 3] = new Pawn("WP");
        piece2[7, 4] = new Pawn("WP");
        piece2[7, 5] = new Pawn("WP");
        piece2[7, 6] = new Pawn("WP");
        piece2[7, 7] = new Pawn("WP");
        piece2[7, 8] = new Pawn("WP");

        piece2[1, 1] = new Rook("BR");
        piece2[1, 2] = new Knight("BKN");
        piece2[1, 3] = new Bishop("BB");
        piece2[1, 5] = new Queen("BQ");
        piece2[1, 4] = new King("BK");
        piece2[1, 6] = new Bishop("BB");
        piece2[1, 7] = new Knight("BKN");
        piece2[1, 8] = new Rook("BR");

        piece2[2, 1] = new Pawn("BP");
        piece2[2, 2] = new Pawn("BP");
        piece2[2, 3] = new Pawn("BP");
        piece2[2, 4] = new Pawn("BP");
        piece2[2, 5] = new Pawn("BP");
        piece2[2, 6] = new Pawn("BP");
        piece2[2, 7] = new Pawn("BP");
        piece2[2, 8] = new Pawn("BP");

        return piece2;
    }  

     public Pieces[,] FirstTimeLoad()
    {
        pieces = ChessBoardDisplay();
        piece = ChessBoardDisplay();
        return pieces;
    }

我在部分类中激活了FirstTimeLoad:

PrintPiecesReplay(codeFileReplay.FirstTimeLoad());
        PictureBox[,] chessPictures = new PictureBox[9, 9];
    public void PrintPiecesReplay(Pieces[,] pieces)
    {
        for (int i = 1; i < 9; i++)
        {
            for (int j = 1; j < 9; j++)
            {
                if (pieces[i, j] is Object)
                {
                    chessPictures[i, j].Load(pieces[i, j].print());
                }
                else
                {
                    chessPictures[i, j].Image = null;
                }
            }
        }
    }

基本上,我从一开始就使用类代码的新对象(Class2)完成整个过程。不会重绘董事会

1 个答案:

答案 0 :(得分:1)

根据PrintPieces,您需要担心重置pieces数组,而不是chessPics数组。