开发者

How to dock to the top and the left

开发者 https://www.devze.com 2023-01-10 15:44 出处:网络
With An anchor I can write the following line: myControl.Anchor = (AnchorStyles.Top | AnchorStyles.Left);

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?


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 and DockStyle.Left would essentially give you DockStyle.Fill. I.e. the same width and height as the container.


A Dock is a pre-determined anchor set, whereas an Anchor is a custom dock configuration.

DockStyle.Top is the same as Anchor = (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.


The DockStyle can only be set to one value, as opposed to the Anchor 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.


May be what you are looking for is the Anchor attribute:

myControl.Anchor = AnchorStyles.Bottom  | AnchorStyles.Right
0

精彩评论

暂无评论...
验证码 换一张
取 消