2013-11-26 21 views
0

我目前正在編寫一個應用程序來通過網絡來玩國際象棋。但是我遇到了一個小問題。更新TableLayoutPanel中的圖片框

TableLayoutPanels cells; 
cells = GetBoard(); 
this.Controls.Add(cells); 

private TableLayoutPanel GetBoard() 
    { 
     TableLayoutPanel b = new TableLayoutPanel(); 
     b.ColumnCount = 8; 
     b.RowCount = 8; 
     for (int i = 0; i < b.ColumnCount; i++) { b.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, Cell.CellSize.Width)); } 
     for (int i = 0; i < b.RowCount; i++) { b.RowStyles.Add(new RowStyle(SizeType.Absolute, Cell.CellSize.Height)); } 
     for (int row = 0; row < b.RowCount; row++) 
     { 
      for (int col = 0; col < b.ColumnCount; col++) 
      { 
       Cell cell = new Cell(row, col); 
       cell.Click += new EventHandler(this.cell_Click); //Added an event handler 
       b.Controls.Add(cell, col, row); 
      } 
     } 
     b.Padding = new Padding(0); 
     b.Size = new System.Drawing.Size(b.ColumnCount * Cell.CellSize.Width, b.RowCount * Cell.CellSize.Height); 

     return b; //Returns the whole table 
    } 

GetBoard初始化8×8的PictureBoxes板。當用戶點擊兩個圖片框時,我有一個鼠標事件處理程序來切換圖片框的圖像。

private void cell_Click(object sender, EventArgs e) 
{ 
    Cell currentClickedCell = (Cell) sender; 
    currentClickedCell.Image = prevClickedCell.Image; //Switch the two images 
    prevClickedCell = null; //Set the previously clicked cell's image to blank 
} 

然後信息將通過ints(通過switch語句強制轉換爲枚舉)通過網絡發送。

但是,我不知道如何更新信息,一旦我得到服務器的響應(兩個整數)。我會盡我所能說出這句話:「有沒有辦法讓兩個單元的」對象發送者「切換圖像?

+0

枚舉不打我作爲編碼棋子位置的好方法。你想編碼「e2到e4」。你可以從0到63的位置編號,很容易適合一個字節,但你永遠不會使用枚舉。你認爲這是通過? –

+0

嗨漢斯,國際象棋邏輯負責人希望它是一個枚舉,所以我只遵循他所說的。 –

回答

0

此代碼將「交換」當前圖像最後一張:

private void cell_Click(object sender, EventArgs e) { 
    Cell currentClickedCell = (Cell)sender; 
    if (prevClickedCell != null) { 
    Image img = currentClickedCell.Image; 
    currentClickedCell.Image = prevClickedCell.Image; 
    prevClickedCell.Image = img; 
    } 
    prevClickedCell = currentClickedCell; 
} 
+0

感謝您的回答!該團隊決定創建另一個陣列,指向電路板上的單元。這允許GUI在沒有任何人工交互的情況下改變。我爲混淆的措辭表示歉意...... –