I am working on a checkerboard in a winform. I have a fixed size board drawing in the form but want to make the checkerboard rescale itself whenever the window is resized. I have a drawBoard function that I am calling in my Paint event. I think I am just missing something easy but I can only get it to work right when I use specific coordinates.
Code:
void Form1_Paint(object sender, PaintEventArgs e)
{
drawBoard(e.Graphics, this.ClientRectangle);
}
private void drawBoard(Graphics g, Rectangle rectangle)
{
bool dark = true;
for (int i = 0; i < 8; i++)
{
dark = !dark;
for (int j = 0; j < 8; j++)
{
dark = !dark;
Pen redPen = new Pen(Color.Red, 2);
SolidBrush brush;
if (dark)
{
brush = new SolidBrush(Color.DarkGray);
}
else
{
brush = new SolidBrush(Color.Red);
}
int x = 50 * i + 10;
int y = 50 * j + 10;
int width = 50;
int height = 50;
//Draw rectangle to screen.
g.DrawRectangle(redPen, x, y, width, height);
g.FillRectangle(brush, x, y, width, height);
//Invalidate (); // redraw the screen
}
}
}