C# WinForm UI 组件层顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1351054/
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
WinForm UI components layer order
提问by Moon
When we add any UI or container in WinForms, the later added component comes over the earlier added components, we can say it is in a higher layer.
当我们在 WinForms 中添加任何 UI 或容器时,后面添加的组件会覆盖在前面添加的组件之上,我们可以说它在更高的层中。
How to change that layer order or component order after adding components?
添加组件后如何更改该层顺序或组件顺序?
采纳答案by Fredrik M?rk
Is it when you load components dynamically in code or in the designer? If it is in the designer you can use the Format -> Order -> Send to Backand Format -> Order -> Bring to Frontcommands. Send to Backand Bring to Frontare also available in the context menu when you right-click a control, as well as in the "Layout" toolbar that should appear automatically when you work in the forms designer.
是在代码中还是在设计器中动态加载组件时?如果它在设计器中,您可以使用Format -> Order -> Send to Backand Format -> Order ->Bring to Front命令。当您右键单击控件时,上下文菜单中以及在表单设计器中工作时应自动出现的“布局”工具栏中也可以使用“置于后部”和“置于前部”。
回答by Gishu
Did you try playing with the Z-Order?
你试过玩Z-Order吗?
See Also: Control.SentToBackmethod - Most Winform controls should thereby support this.
另请参见:Control.SentToBack方法 - 大多数 Winform 控件因此应支持此功能。
回答by Myra
When you place more than one control in the same place,one will end up on top,and the other will end up underneath.Usually this is the result of a minor mistake,such as incorrectly using the anchoring and docking features to creare resizable form.In some cases,however,you might want to overlap control for a specific effect.
当你在同一个地方放置多个控件时,一个会在上面,另一个会在下面。通常这是一个小错误的结果,例如错误地使用锚定和停靠功能来创建可调整大小的表单. 但是,在某些情况下,您可能希望为特定效果重叠控件。
When control overlap,it's the z-index that determines which control ends up on top.Essentially,every control existy in its own distinct numbered layer.A control that has the z-index layer 1 will appear above a control in z-index layer 2 if they overlap.Usually,the z-index of a group of controls is determined by the order in which you add the controls,so that the last control you add is always in the topmost layer (with a z-index of 0).
当控件重叠时,由 z-index 决定哪个控件最终位于顶部。本质上,每个控件都存在于其自己不同的编号层中。具有 z-index 层 1 的控件将出现在 z-index 层中的控件上方2 如果它们重叠。通常,一组控件的 z-index 由添加控件的顺序决定,因此添加的最后一个控件始终位于最顶层(z-index 为 0) .
However, you can change these options.
但是,您可以更改这些选项。
To determine or set the z-index of a control , you can use the GetChildIndex()
and SetChildIndex()
methods of the Controls Collection.Here's an example that moves a control to the third layer in the z-index.
要确定或设置控件的 z-index,您可以使用Controls Collection 的GetChildIndex()
和SetChildIndex()
方法。以下是将控件移动到 z-index 中的第三层的示例。
Controls.SetChildIndex(ctrl, 2);
Usually, you won't need this kind of find-grained control.Instead,you'll just want to drop a control to the back of the z-index (the bottom-most layer) or bring it to the top.You can accomplish this feature at design time by right clicking on a control and choosing Bring to Fron or Send to Back.You can also perform the same task programmatically using the Control.BringToFront()
or Control.SendToBack()
methods.
通常,您不需要这种查找粒度的控件。相反,您只想将控件放到 z-index(最底层)的后面或将其放到顶部。您可以在设计时通过右键单击控件并选择Bring to Fron 或Send to Back 来完成此功能。您也可以使用Control.BringToFront()
或Control.SendToBack()
方法以编程方式执行相同的任务。
ctrl.BringToFront(); // This is equivalent to Controls.SetChildIndex(ctrl,0);
回答by Noam Gal
In the designer view, you can also open up the "Document Outline" panel to view a tree structure of your current form/control, and then drag components around, "up" and "down" to bring to front and back, and also in and out of containers.
在设计器视图中,您还可以打开“文档大纲”面板查看当前窗体/控件的树状结构,然后拖动组件,“向上”和“向下”带到前后,也可以进出容器。