'c# Windows Forms How to edit a list of existing widgets in a loop
I am trying to make a chess game in C# Windows Forms.
I have a 2D array that will represent the board, and what's on it by storing different numbers. Thats also where Ill be doing all of the calculating of the chess pieces movement and weather its a legal move or not.
byte[,] board = new byte[8, 8];
The GUI is a grid of buttons 8x8 (The Board). Each button has a unique name to its position on the board. I have a 2D string array that stores all of these names as follows:
public string[,] TileNames = { {"btn1A","btn1B","btn1C","btn1D","btn1E","btn1F","btn1G","btn1H"},
{"btn2A","btn2B","btn2C","btn2D","btn2E","btn2F","btn2G","btn2H"},
{"btn3A","btn3B","btn3C","btn3D","btn3E","btn3F","btn3G","btn3H"},
{"btn4A","btn4B","btn4C","btn4D","btn4E","btn4F","btn4G","btn4H"},
{"btn5A","btn5B","btn5C","btn5D","btn5E","btn5F","btn5G","btn5H"},
{"btn6A","btn6B","btn6C","btn6D","btn6E","btn6F","btn6G","btn6H"},
{"btn7A","btn7B","btn7C","btn7D","btn7E","btn7F","btn7G","btn7H"},
{"btn8A","btn8B","btn8C","btn8D","btn8E","btn8F","btn8G","btn8H"} };
I made a render function, that would go over each tile in the 2D board array, send the stored number of said tile to 2 functions that return whether its black or white, and what sort of piece it is. ( I present the chess pieces as ascii text on the buttons, and the teams by changing the color of the font )
public void RenderBoard()
{
for (byte col = 0; col < 10; col++)
{
for (byte row = 0; row < 10; row++)
{
Form1._form1.(Button)(_Variables.TileNames[col,row]).ForeColor = UpdateColor(board[col, row]);
Form1._form1.(Button)(_Variables.TileNames[col,row]).Text = UpdateIcon(board[col, row]);
}
}
}
string UpdateIcon(byte value)
{
if (value == _Variables.BlackKing || value == _Variables.WhiteKing) return "♚";
else if (value == _Variables.BlackQueen || value == _Variables.WhiteQueen) return "♛";
else if (value == _Variables.BlackBishop || value == _Variables.WhiteBishop) return "♝";
else if (value == _Variables.BlackKnight || value == _Variables.WhiteKnight) return "♞";
else if (value == _Variables.BlackRook || value == _Variables.WhiteRook) return "♜";
else if (value == _Variables.BlackPawn || value == _Variables.WhitePawn) return "♟";
else return "";
}
Color UpdateColor(byte value)
{
if (value >= 11 && value <= 16) return _Variables.Black;
else if (value >= 21 && value <= 26) return _Variables.White;
else return _Variables.Black;
}
My problem is these 2 lines: Form1._form1.(Button)(_Variables.TileNames[col,row])
Form1._form1.(Button)(_Variables.TileNames[col,row]).ForeColor = UpdateColor(board[col, row]);
Form1._form1.(Button)(_Variables.TileNames[col,row]).Text = UpdateIcon(board[col, row]);
They dont work (obviously thats no way to write down correctly), but its here to represent what Im trying to achieve:
I want to go over each tile in the 2D board array, read what number it has, and represent it accordingly on its matching button tile. I cant find a way to go over each button in parallel to its location in the 2D board array.
I have tried to play with foreach (Control tile in Form1._form1.panelBoard.Controls)
but I cant really go in parallel with the 2D board array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|