C# Panel.Dock Fill 忽略其他 Panel.Dock 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:57:42  来源:igfitidea点击:

Panel.Dock Fill ignoring other Panel.Dock setting

提问by Jeff Cuscutis

If you create a panel on a form and set it to Dock=Top and drop another panel and set its Dock=Fill, it may fill the entire form, ignoring the first panel. Changing the tab order does nothing.

如果您在表单上创建一个面板并将其设置为 Dock=Top,然后放下另一个面板并设置其 Dock=Fill,它可能会填满整个表单,而忽略第一个面板。更改 Tab 键顺序没有任何作用。

采纳答案by JacquesB

Docking layout depends on the orderof sibling controls. Controls are docked "button up", so the last control in the collection is docked first. A docked control only take the layout of previouslydocked siblings into account. Hence the control with Dock=Fill should be first (top) in the sibling order, if you want it to take the other docked controls into account. If it is not the first control, earlier controls will overlap it.

停靠布局取决于同级控件的顺序。控件停靠“按钮向上”,因此集合中的最后一个控件首先停靠。停靠控件仅考虑以前停靠的同级控件的布局。因此,如果您希望将其他停靠的控件考虑在内,则带有 Dock=Fill 的控件应该是兄弟顺序中的第一个(顶部)。如果它不是第一个控件,则较早的控件将与它重叠。

This can be confusing because the sibling-order is not necessarily the same as the visual order, and the sibling order is not always apparent from the design view.

这可能会令人困惑,因为同级顺序不一定与视觉顺序相同,并且同级顺序在设计视图中并不总是很明显。

The Document outlinewindow (View -> Other Windows -> Document outline) gives a useful tree-view over the control hierarchy and order, and allows you to change the sibling order of controls.

文档大纲窗口(视图- >其他窗口- >文件轮廓)给出了一个有用的树视图控件上的层次结构和秩序,并允许您更改控件的同级次序。

You can also change sibling order directly in the designer by context menu -> Bring to front / Send to back, which moves the control to be first or last of the siblings. These menu labels may be somewhat confusing since the actual effect depends on the layout model.

您还可以通过上下文菜单直接在设计器中更改同级顺序 -> 置于前面 / 置于后面,这会将控件移至同级中的第一个或最后一个。这些菜单标签可能会有些混乱,因为实际效果取决于布局模型。

With fixed positioned controls, the 2D position is independent of the sibling order, but when controls are overlapping, the control earliest in the order will be "on top", hiding part of siblings later in the order. In this context Bring to front / Send to back makes sense.

对于固定定位的控件,2D 位置与同级顺序无关,但是当控件重叠时,顺序中最早的控件将在“顶部”,隐藏顺序中较晚的部分同级。在这种情况下,带到前面/发送到后面是有道理的。

Inside flow- or table-layout panels, the creation order determines the visual order of the controls. There is no overlapping controls. So bring to front/send to back really means make first or last in the order of controls.

在流程或表格布局面板中,创建顺序决定了控件的视觉顺序。没有重叠控件。所以放在前面/发送到后面实际上意味着按控件的顺序首先或最后制作。

With docked layout, the bring to front / send to back may be even more confusing since it determines in which order the docking is calculated, so "bring to front" on a fill-docked control will place the control in the middleof the parent, taking all edge-docked controls into account.

使用停靠布局,前移/送后可能会更加混乱,因为它决定了计算停靠的顺序,因此填充停靠控件上的“置于前”会将控件放置在父控件的中间,考虑到所有边缘停靠的控件。

回答by Jeff Cuscutis

Right click on the panel with Dock=Fill and click 'Bring to Front'.

使用 Dock=Fill 在面板上单击鼠标右键,然后单击“置于前面”。

This makes this control be created last, which takes into account the Dock settings on other controls in the same container.

这使得该控件最后创建,这会考虑同一容器中其他控件的 Dock 设置。

回答by John Rudy

Another, potentially cleaner option is to use the TableLayout control. Set up one row of the desired height for your top dock, and another row to fill 100% for your bottom. Set both panels inside to Fill, and you're done.

另一个可能更简洁的选项是使用 TableLayout 控件。为顶部停靠栏设置一行所需高度,并为底部设置另一行以 100% 填充。将里面的两个面板都设置为 Fill,你就完成了。

(TableLayout does take some getting used to, though.)

(不过,TableLayout 确实需要一些时间来适应。)

回答by John Christman

Here is a trick that worked for me..

这是一个对我有用的技巧..

Place the Top item and dock it top.

放置顶部项目并将其停靠在顶部。

Place a Splitter, and also dock it top, then set it disabled (unless you want to resize the top).

放置一个拆分器,并将其停靠在顶部,然后将其设置为禁用(除非您想调整顶部的大小)。

Then Place the Fill object and set Docking to Fill. The object will stay below the splitter.

然后放置填充对象并将停靠设置为填充。对象将停留在分离器下方。

回答by RANONO

JacquesB had the idea with the document outline but the hierarchy didn't solve my problem. My controls were not in a hierarchical style they were just listed with the same parent.

JacquesB 有文档大纲的想法,但层次结构并没有解决我的问题。我的控件不是分层样式,它们只是与同一个父级一起列出。

I learned that if you changed the order it will fix the way you want it to look.

我了解到,如果您更改顺序,它将修复您想要的外观。

The controls on the bottom of the list will overlap the controls on top of it in the Document Outline window. In your case you would make sure that the first panel is below the second panel and so forth.

列表底部的控件将与文档大纲窗口中位于其顶部的控件重叠。在您的情况下,您将确保第一个面板在第二个面板下方,依此类推。

回答by Pandichie Anton-Valentin

I've had the same problem and I managed to solve it.
If you have a container with DockStyle.Fillthe others should also have DockStyle but Top or whatever you want.
The important thing is to add the control with DockStyle.Fillfirst in Controls then the others.

我遇到了同样的问题,我设法解决了它。
如果你有一个容器, DockStyle.Fill其他人也应该有 DockStyle 但 Top 或任何你想要的。
重要的是DockStyle.Fill先在 Controls 中添加控件,然后再添加 其他控件。

Example:

例子:

ComboBox cb = new ComboBox();
cb.Dock =  DockStyle.Top;

GridView gv = new GridView();
gv.Dock =  DockStyle.Fill;

Controls.Add(gv); // this is okay
Controls.Add(cb);

but if we put cb first

但如果我们把 cb 放在第一位

Controls.Add(cb);
Controls.Add(gv); // gv will overlap the combo box.

回答by Marcus

If you don't want to change the order of the elements inside the code, you can use the method Container.Controls.SetChildIndex() with Container being the e.g. Form, Panel etc. you want do add your controls to.

如果您不想更改代码中元素的顺序,您可以使用 Container.Controls.SetChildIndex() 方法,其中 Container 是您想要添加控件的表单、面板等。

Example:

例子:

     //Container ------------------------------------
     Panel Container = new Panel();

     //Top-Docked Element ---------------------------
     ButtonArea = new FlowLayoutPanel();
     Container.Controls.Add(ButtonArea);
     Container.Controls.SetChildIndex(ButtonArea, 1);
     ButtonArea.Dock = DockStyle.Top;

     //Fill-Docked Element --------------------------
     box = new RichTextBox();
     Container.Controls.Add(box);
     Container.Controls.SetChildIndex(box, 0); //setting this to 0 does the trick
     box.Dock = DockStyle.Fill;

回答by Robert S.

I ran into the same issue. Mine was with adding new/custom controls below the menu strip during run time. The problem was the controls when docked, decided to dock from the top of the form and completely ignoring the menu strip entirely, very annoying if you ask me. As this had to be done dynamically with code and not during design mode this became extremely frustrating. The simplest way I found is to create a panel during design mode and dock below the menu strip. From there you can just add/remove the controls to the panel and you can dock it during run time. No need to mess with all your controls on your form that do not really need to change, too much work depending on what you really need to do.

我遇到了同样的问题。我的是在运行时在菜单条下方添加新的/自定义控件。问题是停靠时的控件,决定从表单顶部停靠并完全忽略菜单条,如果你问我的话非常烦人。由于这必须用代码动态完成,而不是在设计模式下完成,这变得非常令人沮丧。我发现的最简单的方法是在设计模式期间创建一个面板并停靠在菜单条下方。从那里,您可以在面板上添加/删除控件,并且可以在运行时停靠它。无需弄乱表单上所有不需要更改的控件,太多工作取决于您真正需要做什么。

object.dock = Fill
Panel.Controls.Add(object)

回答by DrLechter

I know this is an old post but I discovered something useful. To adjust sibling control order programatically for dynamically created control(s), you can do something like:

我知道这是一篇旧帖子,但我发现了一些有用的东西。要为动态创建的控件以编程方式调整同级控件顺序,您可以执行以下操作:

parentForm.Controls.SetChildIndex (myPanel, 0) 

In my case, I did this to move a Dock/Fill panel to be the first control in my form so that it would not overlap with another docked control set to Dock/Top (a menu strip).

就我而言,我这样做是为了将 Dock/Fill 面板移动到我的表单中的第一个控件,这样它就不会与另一个设置为 Dock/Top(菜单条)的停靠控件重叠。