如何停靠到顶部和左侧
使用锚点,我可以编写以下行:
myControl.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
它将把 myControl
锚定到左侧和顶部。
为什么我不能执行以下操作:
myControl.Dock = (DockStyle.Top | DockStyle.Left);
我可以编写上面的行,但它所做的只是将 DockStyle
设置为左侧。
有什么想法/原因吗?
With An anchor I can write the following line:
myControl.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
And it will anchor myControl
to the left and the top.
Why can't I do the following:
myControl.Dock = (DockStyle.Top | DockStyle.Left);
I can write the above line, but all it does is set the DockStyle
to left.
Any thoughts/reasons for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法执行此操作的原因是因为设置
DockStyle
基本上会停靠/填充指定边缘的整体。例如,
DockStyle.Left
表示停靠的项目的高度将始终为容器的高度,并且 X、Y 位置将始终为 0, 0。DockStyle.Top
意味着项目的宽度将始终是容器的宽度,并且位置将始终是 0,0。设置
DockStyle.Top
和DockStyle.Left
本质上会给你DockStyle.Fill
。即与容器的宽度和相同。The reason you cannot do this is because setting a
DockStyle
basically docks/fills the entirity of the specified edge.For example,
DockStyle.Left
means that the height of the item being docked will always be the height of the container and the the X,Y location will always be 0, 0.DockStyle.Top
means that the width of the item will always be the width of the container and the location will always be 0,0.Setting
DockStyle.Top
andDockStyle.Left
would essentially give youDockStyle.Fill
. I.e. the same width and height as the container.Dock
是预先确定的锚点集,而Anchor
是自定义的锚点配置。DockStyle.Top
与Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right)
相同,只是锚点可以位于任何初始位置,而停靠点将位于任意初始位置。移动到最远的边缘。A
Dock
is a pre-determined anchor set, whereas anAnchor
is a custom dock configuration.DockStyle.Top
is the same asAnchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right)
except that an anchor can sit at any initial position and a dock will move to the far edge.DockStyle
只能设置为一个值,而Anchor
可以设置为多个值。这就是为什么有
Anchor
属性,以便您可以更具体地调整控件对表单大小调整的反应方式。The
DockStyle
can only be set to one value, as opposed to theAnchor
that can be set to many.That is why there is the
Anchor
property so that you can adjust how the control reacts to the form resizing more specifically.您正在寻找的可能是
Anchor
属性:May be what you are looking for is the
Anchor
attribute: