0

I have a "layout" database which has records for a grid of buttons: the top, left, width & height values are kept small and I multiply these upon drawing them to the form to scale them up. The grid makes up the full form and the form is full screen however my scale factor is set for the form size I created them at. I'd like to be able to determine the values to scale them by in order for me to scale the button grid to any size window regardless of aspect ratio with the grid still taking up the full screen.

I'd like to avoid docks, anchor's and table layouts as I believe this adds complication as the form cannot be resized once shown anyway.

I have looked at the Control.Scale Method mentioned in this answer but I'm not sure how to apply it.

Community
  • 1
  • 1
  • Well, probably not what you want to hear but WPF is here because of these shortcomings of WinForms. – Steve Dec 03 '16 at 08:58
  • 1
    @Steve, do not give up on Winforms so quickly :) – Fabio Dec 03 '16 at 09:18
  • @Fabio far from me. Working in this same moment on a big winform project – Steve Dec 03 '16 at 09:20
  • [How to create a magic square using Windows Forms?](https://stackoverflow.com/questions/33968993/how-to-create-a-magic-square-using-windows-forms) – Reza Aghaei Dec 03 '16 at 10:58
  • You will need to allow for a) some slack at the right and bottom for integer remainders and b) some slack for font sizes. but other than that, where is your actual problem? Note that you must not use the From.Size but Form.ClientSize! – TaW Dec 03 '16 at 14:46

1 Answers1

3

Instead of calculating values use TableLayoutPanel control.

  • Add TableLayoutPanel to the form
  • Set TableLayourPanel.Dock = Fill
  • Add columns and rows as you need
  • Set columns and rows SizeType to "Percent" and percent values to fit your needs.
  • Then add buttons into cells you have created
  • Set Button.Dock = Fill to fill whole cell.

In runtime when form's size will change, size and position of the buttons will changes too.

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Great answer, but my button's don't follow a set number of columns and rows - they are freely arranged within a given space and I need to be able to position them wherever I need but for the whole thing to scale. –  Dec 03 '16 at 10:05
  • @OMTRX, you can arrange buttons in same "free" way with `TableLayoutPanel` too. If you show some specific example with reason which prevent of using `TableLayoutPanel` – Fabio Dec 03 '16 at 10:15
  • That I doubt. They still need to be inside the grid 'cells', so if some fit but others overlap that grid TLP will not work.. – TaW Dec 03 '16 at 14:44
  • 1
    @OMTRX, they are still inside cells but there can be grids and grids inside grids. – Fabio Dec 03 '16 at 15:25