简体   繁体   中英

How to dock to the top and the 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 . Ie 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.

你可能正在寻找的是Anchor属性:

myControl.Anchor = AnchorStyles.Bottom  | AnchorStyles.Right

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM