7

How does one specify that a button centers itself in a container without having to specify a Location? Is it even possible?

The goal is to be able to center multiple buttons in a panel without having to perform calculations on their placement.

Matthew Cox
  • 13,566
  • 9
  • 54
  • 72
  • What should happen if 2 buttons said that they wanted to be centered? – McKay Nov 17 '11 at 20:32
  • Small amount of margin between them. Like how float would behave in Css – Matthew Cox Nov 17 '11 at 20:33
  • @MatthewCox how about using Anchor property of buttons in side panel... – Glory Raj Nov 17 '11 at 20:34
  • look at this might be a dup alltough I wouldnt vote for a close cause there isn't really an answer in it. http://stackoverflow.com/questions/942211/how-do-i-center-controls-without-resizing-them-net-winforms – albertjan Nov 17 '11 at 20:35
  • @errorstacks Anchor does not appear to behave in that manner. It seems to be more like tell it to remain fixed relative to what you specify for the anchor and its location. – Matthew Cox Nov 17 '11 at 20:39
  • 1
    Center the control yourself, but then remove the left and top anchors (which are default on) so that there are NO anchors. – LarsTech Nov 17 '11 at 20:42
  • @LarsTech That was the point of the question. Can it be done WITHOUT centering it myself. Clearly the answer in No. – Matthew Cox Nov 17 '11 at 20:47
  • 1
    If at design-time, Otiel's answer is correct. But at runtime? I think you would have to calculate that yourself, especially since multiple controls could be involved. – LarsTech Nov 17 '11 at 20:51
  • 1
    Possible duplicate of [Centering controls within a form in .NET (Winforms)?](http://stackoverflow.com/questions/491399/centering-controls-within-a-form-in-net-winforms) – Jim Fell May 19 '16 at 18:50

2 Answers2

19

I know it is possible to center some controls on a form, not sure about a panel though. Anyway:

  • Disable the Left and Right anchors of your control if you want your controls to stay centered horizontally, and the Top and Bottom anchors if you want your controls to stay centered vertically,
  • In the designer window, select your control,
  • In the VS 'Format' menu, hit 'Center in form', then 'Horizontally' and/or 'Vertically'.

If you want to center several controls side to side, select them all and execute the above steps.

Controls will then stay centered in the form when the user resizes the window.

Otiel
  • 18,404
  • 16
  • 78
  • 126
  • 1
    You have it right, but I think you meant "Left and Top" anchors. – LarsTech Nov 17 '11 at 20:45
  • @Otiel This doesn't behave as I need. In the end, if you examine the `InitializeComponent()` It is still specifying a calculated Location then giving it no anchors. – Matthew Cox Nov 17 '11 at 20:45
  • @Otiel Very nice to know when the position should remain fixed and is known at compile-time. However, just doesn't work in my case. – Matthew Cox Nov 17 '11 at 20:46
  • @LarsTech: I actually meant `Left` and `Right` because I was thinking of center *horizontally* when I wrote the answer. But you're right, `Top` and `Bottom` should be also disabled to center *vertically*. Answer updated. – Otiel Nov 17 '11 at 20:47
2

I'm not 100% sure of what you are asking, but try using a TableLayoutPanel, and drop one button in each cell of the table. If you anchor the TableLayoutPanel to the Top, Left, Bottom& Right, the Table will grow and shrink with the form, but each button will "float" relative to the top-left corner of it's containing cell.

Disabling all anchoring will keep the TableLayoutPanel at it's relative location within your form, but your buttons will remain spaced out evenly amongst one another.

Between standard control anchoring and/or the the TableLayoutPanel you should be able to find the correct type of anchoring that you desire.

RLH
  • 15,230
  • 22
  • 98
  • 182