windows 动态识别控件的容器(GroupBox 等)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4468365/
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-09-15 15:48:56  来源:igfitidea点击:

Identifying the control's container (GroupBox, etc.) dynamically

c#.netwindowswinforms

提问by JCTLK

I'm dynamically generating a Windows Forms form by reading an XML file. (Actually I have a separate tool developed to serialize a Windows Forms form and its components.)

我通过读取 XML 文件动态生成 W​​indows 窗体表单。(实际上,我开发了一个单独的工具来序列化 Windows 窗体窗体及其组件。)

How do I find out in which container control each control is in?

如何找出每个控件所在的容器控件?

For example, if I'm dynamically creating a label I want to find if it is inside a group box or any container. Is there a property available for it?

例如,如果我正在动态创建一个标签,我想查找它是否在组框或任何容器内。是否有可用的属性?

回答by Adriaan Stander

回答by BeemerGuy

That would be mycontrol.Parent.

那将是mycontrol.Parent

回答by ESLtemp

The controls of .NET are having a property called Parent. The controls inside the group panel are set to their parent as the Group box name.

.NET 的控件有一个名为 Parent 的属性。组面板内的控件设置为其父项作为组框名称。

回答by Sree

First, you can find a group box, say for example:

首先,您可以找到一个分组框,例如:

groupbox x

After that, find a label, like

之后,找到一个标签,比如

(Label) x.findControl("labelID"). 

You can try it like this.

你可以这样试试。