C# 获取父级容器控件内容:使用 Parent 和 Controls 属性
C# 获取父级容器控件内容
可以使用以下代码获取按钮的父级容器中的指定控件内容:
Control parent = button1.Parent; // 获取父级容器
Control targetControl = parent.Controls['targetControlName']; // 获取指定的控件
其中,'targetControlName' 为目标控件的名称。如果目标控件不存在,targetControl 将为 null。
获取自定义控件内容
如果目标控件是一个自定义控件,可以通过类型转换来获取该控件的实例:
CustomControl targetControl = parent.Controls['targetControlName'] as CustomControl;
if(targetControl != null) {
// 执行操作
}
注意:
- 以上代码中的 'button1' 代表按钮控件。
- 'CustomControl' 代表您的自定义控件类名。
- 如果目标控件不存在,需要进行 null 判断以避免错误。
原文地址: https://www.cveoy.top/t/topic/oUkb 著作权归作者所有。请勿转载和采集!