Keeping winform control centered after window resize

南楼画角 提交于 2019-12-12 09:33:02

问题


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.



来源:https://stackoverflow.com/questions/3593411/keeping-winform-control-centered-after-window-resize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!