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"

2
  • 5
    It is explicitly mentioned in the linked article. Number 8, Anchor = None. Sep 27, 2012 at 15:30
  • And @HansPassant, while #8 #3 should work.. they don't.
    – sammarcow
    Sep 27, 2012 at 15:32

2 Answers 2

53

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.

7
  • 1
    Unfortunately I tried doing this after reading the technical reference and it did not work.
    – sammarcow
    Sep 27, 2012 at 15:29
  • @sammarcow 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.
    – LarsTech
    Sep 27, 2012 at 15:44
  • 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.
    – sammarcow
    Sep 27, 2012 at 20:27
  • @sammarcow Your images show you have Anchors set to Top, Left, Right.
    – LarsTech
    Sep 27, 2012 at 20:32
  • 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.
    – sammarcow
    Oct 1, 2012 at 15:22
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

1
  • Added screenshot and technical reference.
    – sammarcow
    Sep 27, 2012 at 15:07

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.