views:

50

answers:

3

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?

+1  A: 

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.

w69rdy
This is true, but what is the reason for this restriction on the docking and is there a way around it?
AidanO
I don't know a specific reason why, I think the Dock is just for simple, easy docking of controls and wouldn't work having multiples (you couldnt have a dock style fill AND none for example) so thats why there is the Anchor as well.
w69rdy
A: 

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.

Codesleuth
They're kinda similar, but they're not the same. Anchoring allows you set locations away from the edges of the container. Docking forces the docked control to the specified edge. I.e. Left = 0. Top = 0.
GenericTypeTea
@GenericTypeTea I did say this in my answer
Codesleuth
@Codesleuth - So you did. My apologies, I apparently didn't read it properly.
GenericTypeTea
+4  A: 

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.

GenericTypeTea
Thanks Gtt, that explains what I'm seeing. I may have to pose another question about what I'm trying to achieve once I figure out how to phrase it!
AidanO
No problem. I've been through all the pain of docking and anchoring myself, so I'll keep an eye out for the next question.
GenericTypeTea