14

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?

Alex Humphrey
  • 6,099
  • 4
  • 26
  • 41
AidanO
  • 868
  • 1
  • 11
  • 33
  • i think you just didn't fully understand how to get the desired layout by using dock, anchor and some kind of container control (like TableLayoutPanel). With these elements you're able to built every kind of layout that will nicely scale with the size of your form/control. So if you provide more informations about what you like to achieve in your layout i'm sure we can find a solution. – Oliver Aug 09 '10 at 14:22

4 Answers4

16

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.

djdd87
  • 67,346
  • 27
  • 156
  • 195
  • 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 Aug 09 '10 at 14:16
  • 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. – djdd87 Aug 09 '10 at 14:17
5

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
  • 10,321
  • 8
  • 51
  • 71
  • 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. – djdd87 Aug 09 '10 at 14:03
  • @GenericTypeTea I did say this in my answer – Codesleuth Aug 09 '10 at 14:19
  • So you did. My apologies, I apparently didn't read it properly. – djdd87 Aug 09 '10 at 14:27
2

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.

Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • This is true, but what is the reason for this restriction on the docking and is there a way around it? – AidanO Aug 09 '10 at 13:59
  • 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. – Iain Ward Aug 09 '10 at 14:05
0

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

myControl.Anchor = AnchorStyles.Bottom  | AnchorStyles.Right