Reputation: 914
Well the title pretty much says it all...I'm building a C# WinForms chess game and am trying to implement the ability to resize the board.
I currently have a form with FormBorderStyle
set to Sizable
and this TableLayoutPanel
on it, as well as this Layout
event handler.
private void Form1_Layout(object sender, LayoutEventArgs e)
{
panBoard.SuspendLayout();
int xDiff = this.Size.Width - oldSize.Width;
int yDiff = this.Size.Width - oldSize.Height;
int diff = xDiff > yDiff ? xDiff : yDiff;
this.Size = new Size(oldSize.Width +
diff, oldSize.Height + diff);
sizeXY += diff;
game.SizeXY = sizeXY;
panBoard.ResumeLayout();
}
When I run the app and resize the form, the background image gets scaled slightly too much, as shown by these two pictures. The TableLayoutPanel
's cell borders should line up nicely with the cells in the image. Any ideas about what I'm doing wrong? MTIA :-)
Upvotes: 1
Views: 1027
Reputation: 11717
Yep....Using those pictures is definitely a better solution. But do allow me to also show off a dirty trick that I used few years back for an application I made for myself.
I took a form and added a TableLayoutPanel
in it. Set TableLayoutPanel.Dock
property to Fill
. Set the Form size to be a square (400 * 400 for ex.). Set TableLayoutPanel
columns and rows as 8 and set width as Percentage
to 50 to all. Then I would like to generate Panel
at runtime and add them to each box in TableLayoutPanel
by using a logical for
loop. Change the BackColor
of each alternate panel at runtime (to appear them as chess). Set Dock
property of each panel to Fill
. You can play with Margin
property of panel to look them better. Run your aplication and everything should as expected.
Few advantages of this trick is, you can easily change the color of your chess board anytime. Number of boxes in TableLayoutPanel
can also be changed (if its not chess). I know this is something which you were not looking for, but definitely something you can consider. It will not make your application heavy as well. And trust me, the output looks cool.
Hope it helps.
Upvotes: 1
Reputation: 150178
Your picture looks like the image is being scaled to the entire size of the form, rather than just to the area of the chess board.
Rather than using
this.Size
as a basis for scaling, try using
this.ClientSize
The size of the client area of the form is the size of the form excluding the borders and the title bar.
http://msdn.microsoft.com/en-us/library/9278sfx2(v=vs.110).aspx
Upvotes: 2