-4

Is there a way to create Windows Forms controls randomly in a grind style?

Example picture without grid: https://i.stack.imgur.com/fh1Hs.png

That or maybe with a like 32x32 grid where the buttons are placed but don't intersect with each other, all cells are filled and maybe there is some space between each cell?

Is there a method I should take a look at?

1 Answers1

1

I can think of 2 possiblities to do what you're asking for:

1- TableLayoutPanel

More infos: Adding controls to TableLayoutPanel dynamically during runtime

2- Playing with locations

Example:

// Start with 2 to have margins in the top/left
int cX = 2;
int cY = 2;

for (int x = 0; x < 8; x++)
{
    for (int y = 0; y < 8; y++)
    {
        var btn = new Button();
        btn.Location = new Point(cX, cY);
        btn.Size = new Size(32, 32);
        Controls.Add(btn);
        cX += 34;
    }

    cY += 34;
    cX = 2;
}

This will create a 8x8 grid of 32x32 buttons with a space of 2 pixels.

Haytam
  • 4,643
  • 2
  • 20
  • 43