根据指定的容器和控件名称获得控件

代码
/// <summary>
/// 根据指定容器和控件名字,获得控件
/// </summary>
/// <param name="obj">容器</param>
/// <param name="strControlName">控件名字</param>
/// <returns>控件</returns>
private object GetControlInstance(object obj,string strControlName)
{
IEnumerator Controls
= null;//所有控件
Control c = null;//当前控件
Object cResult=null;//查找结果
if(obj.GetType() == this.GetType())//窗体
{
Controls
= this.Controls.GetEnumerator();
}
else//控件
{
Controls
= ((Control)obj).Controls.GetEnumerator();
}
while(Controls.MoveNext())//遍历操作
{
c
= (Control)Controls.Current;//当前控件
if(c.HasChildren)//当前控件是个容器
{
cResult
= GetControlInstance(c,strControlName);//递归查找
if(cResult==null)//当前容器中没有,跳出,继续查找
continue;
else//找到控件,返回
return cResult;
}
else if(c.Name == strControlName)//不是容器,同时找到控件,返回
{
return c;
}
}
return null;//控件不存在
}

本文来自CSDN博客,转载请标明出处:http:
//blog.csdn.net/wangxianshou/archive/2010/08/24/5835145.aspx


返回导读目录,阅读更多随笔



分割线,以下为博客签名:

软件臭虫情未了
  • 编码一分钟
  • 测试十年功


随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

原文地址:https://www.cnblogs.com/08shiyan/p/1833007.html