Question Grid

Kissen

Member
Joined
Jun 20, 2019
Messages
13
Programming Experience
Beginner
I created an array of buttons that should create a square of size gridSize. But I have no Idea how to make the buttons go where they are supposed to, they always overlap
C#:
   Button[,] tiles = new Button[gridSize,gridSize];
            int buttonSize = this.ClientRectangle.Width / gridSize;
            for(int x =0; x<gridSize; x++)
            {
                for(int y= 0; y<gridSize; y++)
                {
                    tiles[x, y] = new Button();
                    tiles[x, y].Size = new Size(buttonSize, buttonSize);
                    tiles[x, y].Location = new Point(gridSize*x, y*gridSize);
                    this.Controls.Add(tiles[x,y]);
 
Before you add a new control in your loop, set its position first. For each button increase the values of x y location by a factor number. Be sure they stay inside the compond of your form.
 
Shouldn't the tiles location depend on the button size, and not the grid size? x/y is already the variable for place in grid.
 
Back
Top Bottom