3

How do i dock a child control at a bottom right position when compared to the parent control?

I can see that the dockstyle enum has values for None,Top,Bottom,Right,Left and Fill ...

How can i set for Bottom right ???

General Grey
  • 3,598
  • 2
  • 25
  • 32
Nick
  • 586
  • 2
  • 7
  • 22

5 Answers5

7

perhaps you don't want to dock it bottom-right. Docking changes the position of the control, but also the size to fit in th height or width of the form.

If you want to keep it down and on the right, anchor it.Remove left and top anchors and add bottom and right anchors. Your control will keep there!

** EDIT ** According to OP comment, it must be on the bottom and take all width and have fixed height. then you must take this steps:

To keep it tidy, you need at least 2 controls:

  • The one that it's on the bottom: dock it to the bottom and set its height.
  • Other one that use docking style of Fill. This makes it take all the space not occupied by the bottom control.

If you have problems setting it up, use the Layout Window (I hope that's the name in English. My VS is localized) to move them around until it works. Sometimes docking it's a bit nasty and the only way to make it work the way you like is changing the order nad nesting of controls using this layout window.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Thats exactly what i want . i need the child control to fit within the height and width of the parent form ... if i set the anchorstyle to bottom right , i m unable to see the control, i m guessing its going beyond the bounds of the parent control.... – Nick May 24 '12 at 15:03
  • Please define it more exactly. Do you want it on the bottom, taking all the width or on the right taking all the height? if you help me to understand it I'll improve my answer. – JotaBe May 24 '12 at 15:10
  • I believe JotaBe is Correct, you need to set your anchor to Bottom right and locate it exactly where you want it to stay, when you resize your Form it will stay relative to the bottom right corner – General Grey May 24 '12 at 15:20
  • i want it to be on the right , taking all the width of the parent control but should be able to define height... – Nick May 24 '12 at 15:20
  • well then dock to bottom, that will take all the width of the parent control be able to define the height, What is the Control you are adding though, you may have to turn AutoSize=false – General Grey May 24 '12 at 15:24
  • @JotaBE His control is a Label, dock to bottom align to right Autosize false, I think it is a label anyway, so confusing when people don't give you all the information you need – General Grey May 24 '12 at 15:31
3

Use AnchorStyles:

yourComponent.Anchor = ((System.Windows.Forms.AnchorStyles)
                       ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
sebi
  • 1,791
  • 3
  • 25
  • 43
1

To "dock" in the bottom right, you need to

  1. Dock ControlA on the Right side of the parent, ControlB
  2. Set the Top Padding of ControlA to ControlA.Padding = new Padding(0, ControlB.Height - nTopPadding, 0, 0);

nTopPadding can be whatever you need it to be. For TextBoxes, Labels, and the like, ControlA.Font.Height works the best.

This also works when AutoSize = true. You'll only need to update the padding as needed.

DCOPTimDowd
  • 231
  • 1
  • 11
0

From MSDN documentation for Control.Dock:

A control can be docked to one edge of its parent container or can be docked to all edges and fill the parent container.

So you cannot dock to two edges - I'm actually not sure what you mean by this.

If you want to keep a control in the bottom right of the screen you might be thinking of the Anchor property, which does let you set multiple edges to anchor the control to.

raveturned
  • 2,637
  • 24
  • 30
  • Okie.. thanks for the info, let me dock the control bottom and align the label field to right... – Nick May 24 '12 at 15:17
0

try setting the Dock to Bottom, Depending on your control you may have to turn autosize off, a label for instance

General Grey
  • 3,598
  • 2
  • 25
  • 32