简体   繁体   English

如何将子控件停靠在父控件的右下角?

[英]How to dock a child control to bottom right of the parent control?

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 ... 我可以看到dockstyle枚举的值为None,Top,Bottom,Right,Left和Fill ...

How can i set for Bottom right ??? 我如何设置右下角的???

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. **编辑**根据OP注释,它必须在底部并采用所有宽度并具有固定高度。 then you must take this steps: 那么您必须执行以下步骤:

To keep it tidy, you need at least 2 controls: 为了保持整洁,您至少需要2个控件:

  • 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. 另一种使用对接样式的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. 如果您在设置时遇到问题,请使用“布局窗口”(我希望这是英文名称。我的VS已本地化)来移动它们,直到它起作用为止。 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. 有时,将其停靠有点麻烦,使它按自己喜欢的方式工作的唯一方法是使用此布局窗口更改控件的order nad嵌套。

Use AnchorStyles: 使用AnchorStyles:

yourComponent.Anchor = ((System.Windows.Forms.AnchorStyles)
                       ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));

From MSDN documentatio n for Control.Dock : MSDN文档中的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. 如果要将控件保留在屏幕的右下方,则可能需要考虑Anchor 属性 ,该属性确实允许您设置多个边缘以将控件锚定到该属性

尝试将Dock设置为Bottom,根据您的控件,您可能需要关闭自动尺寸调整功能,例如标签

To "dock" in the bottom right, you need to 要在右下角“停靠”,您需要

  1. Dock ControlA on the Right side of the parent, ControlB 将ControlA停靠在父级的右侧 ControlB
  2. Set the Top Padding of ControlA to ControlA.Padding = new Padding(0, ControlB.Height - nTopPadding, 0, 0); 将ControlA的顶部填充设置为ControlA.Padding = new Padding(0, ControlB.Height - nTopPadding, 0, 0);

nTopPadding can be whatever you need it to be. nTopPadding可以是您需要的任何内容。 For TextBoxes, Labels, and the like, ControlA.Font.Height works the best. 对于TextBoxes,Label等, ControlA.Font.Height效果最好。

This also works when AutoSize = true . AutoSize = true时,这也适用。 You'll only need to update the padding as needed. 您只需要根据需要更新填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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