如何使形式的面板中心? [英] How to make a panel center of form?

查看:89
本文介绍了如何使形式的面板中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使形式的面板中心,即使窗体的大小被改变。
使用C#Windows应用程序

How to make a panel center of form, even if the size of the form is changed. Using c# windows application

推荐答案

位置使用设计器,然后在窗体中央面板清除属性,因此它不挂靠任何优势。这将保持在窗体调整大小,而不调整面板本身它为中心。

Position the panel in the center of the form using the designer, and then clear the Anchor property, so it is not anchored to any edge. This will keep it centered when the form resizes, without resizing the panel itself.

如果由于某种原因,你需要在代码中的面板位置(这取决于事情例如窗体加载过程中发生),你可以做这样的事情:

If, for some reason, you will need to position the panel in code (depending on things that happens during form load for instance) you can do something like this:

// code for initializing the panel and setting 
// its size goes here

_thePanel.Location = new Point(
    this.ClientSize.Width / 2 - _thePanel.Size.Width / 2,
    this.ClientSize.Height / 2 - _thePanel.Size.Height / 2);
_thePanel.Anchor = AnchorStyles.None;

这应该照顾大多数情况下,我想。

That should take care of most scenarios, I imagine.

这篇关于如何使形式的面板中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆