Reputation:
I want to draw a gridlayout based on a given number of known squares. Example, 16 squares, makes 8x8 grid. But my grid looks a little bit odd, can't help it :(
edit: added wrong picture in first place!
int n = 16;
int grid = (int)Math.Sqrt(n);
int x = 0, y = 0;
int yCounter = 0;
int xCounter = 0;
for (int i = 0; i < n; i++)
{
myGeometricObject[i] = new GeometricObject();
x = xCounter * 50;
xCounter++;
if(i % grid == 0 && i > 0)
{
yCounter++;
xCounter = 0;
y = yCounter * 50;
}
myGeometricObject[i].Location = new System.Drawing.Point(x, y);
myGeometricObject[i].Size = new System.Drawing.Size(50, 50);
this.Controls.Add(myGeometricObject[i]);
}
Upvotes: 0
Views: 1677
Reputation: 1258
2 lines have to be moved, like this:
int n = 16;
int grid = (int)Math.Sqrt(n);
int x = 0, y = 0;
int yCounter = 0;
int xCounter = 0;
for (int i = 0; i < n; i++)
{
myGeometricObject[i] = new GeometricObject();
if(i % grid == 0 && i > 0)
{
yCounter++;
xCounter = 0;
y = yCounter * 50;
}
// Next 2 lines
x = xCounter * 50;
xCounter++;
myGeometricObject[i].Location = new System.Drawing.Point(x, y);
myGeometricObject[i].Size = new System.Drawing.Size(50, 50);
this.Controls.Add(myGeometricObject[i]);
}
Oh, see that you've solved it youself. Always good!
Upvotes: 0
Reputation:
Solved, correct Code stated as following
int n = 16;
int grid = (int)Math.Sqrt(n);
int x = 0, y = 0;
int yCounter = 0;
int xCounter = 0;
for (int i = 0; i < n; i++)
{
myGeometricObject[i] = new GeometricObject();
if (i % grid == 0)
{
y = yCounter * 50;
yCounter++;
xCounter = 0;
}
else
{
xCounter++;
}
x = xCounter * 50;
myGeometricObject[i].Location = new System.Drawing.Point(x, y);
myGeometricObject[i].Size = new System.Drawing.Size(50, 50);
this.Controls.Add(myGeometricObject[i]);
}
Upvotes: 1