Centering Windows Forms Controls inside TableLayoutPanel with Visual Studio Designer
Asked Answered
S

2

15

I have control containers tightly bound to the edge of user controls, evenly spaced on both sides of said user control (child controls are always centered within the parent). Using the designer, I am attempting to add these user controls which that spans all columns within a TableLayoutPanel. I am clicking on the control container and clicking the "Center Horizontally" button on the "Layout" Toolbar. The control will not center.

Why? How do easily center the control?

What I am experiencing does not align with the documentation How to: Align and Stretch a Control in a TableLayoutPanel Control

Problem Control is the 5 Buttons which should be centered among the 2 spanned columns shown:

Buttons are tightly bound to edge of control: Control Layout

New image showing anchors properly set to "Left, Right" on suspect control New image showing anchors

New image showing anchors set to "None" Anchors set to "None"

Skaw answered 26/9, 2012 at 20:11 Comment(2)
It is explicitly mentioned in the linked article. Number 8, Anchor = None.Trever
And @HansPassant, while #8 #3 should work.. they don't.Skaw
B
55

Try setting the Anchor property to None:

dataMasterControl1.Anchor = AnchorStyles.None;

It should center itself from within the TableLayoutPanel cells that contains the control. You may have to adjust the size of the UserControl itself.

Blindstory answered 27/9, 2012 at 15:16 Comment(7)
Unfortunately I tried doing this after reading the technical reference and it did not work.Skaw
@Skaw I tested this with a UserControl with two buttons spanning two columns in my TableLayoutPanel and it centered just fine. Like I mentioned, make sure to adjust the size of the UserControl — it should be just a bit wider than the last button's right side.Blindstory
I updated the post @LarsTech. I do not know why, but the additional pictures I had added did not appear. you can see from the pictures I cannot get the results you are describing. I am happy to provide the full source.Skaw
@Skaw Your images show you have Anchors set to Top, Left, Right.Blindstory
I updated the picture showing the problem with the anchors set to left, right. Then, added another picture to show the same effect with Anchors set to None.Skaw
@Skaw The size of the control looks like it is filling the entire area of the cells. Try changing the size of the UserControl.Blindstory
This worked for me, I set the controls all to "dock : fill", set the "minimum" and "maxium" size to the size I wanted. Then I took the "left" & "top" anchor off so there was no more anchor. The minimum size stopped the buttons from shrinking to nothingForedo
S
0

I know this is not a full answer but have you tried to adjust the row, column and cell sizes at all? It would be helpful if you would post a screen shot maybe?

http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel(v=vs.80).aspx

Salpingectomy answered 26/9, 2012 at 21:1 Comment(1)
Added screenshot and technical reference.Skaw

© 2022 - 2024 — McMap. All rights reserved.