Keeping winform control centered after window resi

2019-04-17 23:29发布

问题:

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.

回答1:

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