Keeping winform control centered after window resize
Asked Answered
D

1

35

Using Visual Studio 2008, windows forms, C#, .NET 2.0 ...

Is there a code-less way to get a control (it happens to be a PictureBox in my case) to stay centered as the window is resized? In other words, using some combination of property settings rather than writing code by hand to keep the element centered.

For example, there's a button in the center of a windows forms window, when the user resizes the window, the button will stay fixed in place based on its default anchoring (top,left). What I'd like it to do is to stay centered, regardless of how the window is resized.

I'm aware that I could watch for a window resize event and then have all elements on the form reposition themselves to stay in their same relative positions (based on code I would write to have them do that). I'm hoping that there is some combination of property settings to acheive the same effect.

As suggested in other posts, I've tried using the TableLayout control, but could use a shove in the right direction in terms of the specific solution to centering an element.

Ducktail answered 29/8, 2010 at 3:33 Comment(1)
Possible duplicate of Centering controls within a form in .NET (Winforms)?Tillman
D
78

Center your control in the designer and set its Anchor property to None.

Dougald answered 29/8, 2010 at 5:5 Comment(1)
Wow, that was intuitive! </sarcasm>Hoff

© 2022 - 2024 — McMap. All rights reserved.