实现按名称递归查找控件的方法WinForm
来源:互联网 发布:大数据面临的挑战 编辑:程序博客网 时间:2024/05/17 07:16
本文所述实例主要实现了WinForm实现按名称递归查找控件的功能,在C#项目开发中有一定的应用价值,分享给大家供大家参考借鉴。
关键代码如下:
/// <summary>
/// 向下递归查找控件
/// </summary>
/// <param name="parentControl">查找控件的父容器控件</param>
/// <param name="findCtrlName">查找控件名称</param>
/// <returns>若没有查找到返回NULL</returns>
public static Control DownRecursiveFindControl(this Control parentControl, string findCtrlName)
{
Control _findedControl = null;
if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)
{
foreach (Control ctrl in parentControl.Controls)
{
if (ctrl.Name.Equals(findCtrlName))
{
_findedControl = ctrl;
break;
}
else
{
if (ctrl.Controls.Count > 0)
_findedControl = DownRecursiveFindControl(ctrl, findCtrlName);
}
}
}
return _findedControl;
}
/// <summary>
/// 将Control转换某种控www.wolaix.com件类型
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="control">Control</param>
/// <param name="result">转换结果</param>
/// <returns>若成功则返回控件;若失败则返回NULL</returns>
public static T Cast<T>(this Control control, out bool result) where T : Control
{
result = false;
T _castCtrl = null;
if (control != null)
{
if (control is T)
{
try
{
_castCtrl = control as T;
result = true;
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("将Control转换某种控件类型异常,原因:{0}", ex.Message));
result = false;
}
}
}
return _castCtrl;
}
测试代码如下:
bool _sucess = false;
CheckBox _finded = this.DownRecursiveFindControl("checkBox1").Cast<CheckBox>(out _sucess);
if (_sucess)
{
MessageBox.Showww.fanyaylc.comw(_finded.Name);
}
else
{
MessageBox.Show("Not Finded.");
}
希望本文所述实例能够对大家的C#程序设计有所帮助!
关键代码如下:
/// <summary>
/// 向下递归查找控件
/// </summary>
/// <param name="parentControl">查找控件的父容器控件</param>
/// <param name="findCtrlName">查找控件名称</param>
/// <returns>若没有查找到返回NULL</returns>
public static Control DownRecursiveFindControl(this Control parentControl, string findCtrlName)
{
Control _findedControl = null;
if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)
{
foreach (Control ctrl in parentControl.Controls)
{
if (ctrl.Name.Equals(findCtrlName))
{
_findedControl = ctrl;
break;
}
else
{
if (ctrl.Controls.Count > 0)
_findedControl = DownRecursiveFindControl(ctrl, findCtrlName);
}
}
}
return _findedControl;
}
/// <summary>
/// 将Control转换某种控www.wolaix.com件类型
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="control">Control</param>
/// <param name="result">转换结果</param>
/// <returns>若成功则返回控件;若失败则返回NULL</returns>
public static T Cast<T>(this Control control, out bool result) where T : Control
{
result = false;
T _castCtrl = null;
if (control != null)
{
if (control is T)
{
try
{
_castCtrl = control as T;
result = true;
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("将Control转换某种控件类型异常,原因:{0}", ex.Message));
result = false;
}
}
}
return _castCtrl;
}
测试代码如下:
bool _sucess = false;
CheckBox _finded = this.DownRecursiveFindControl("checkBox1").Cast<CheckBox>(out _sucess);
if (_sucess)
{
MessageBox.Showww.fanyaylc.comw(_finded.Name);
}
else
{
MessageBox.Show("Not Finded.");
}
希望本文所述实例能够对大家的C#程序设计有所帮助!
0 0
- 实现按名称递归查找控件的方法WinForm
- 折半查找的递归与非递归方法实现
- 二分法查找的实现 递归与非递归方法
- C# 查找指定名称的控件
- WinForm递归遍历控件
- java实现的递归方法逆序对查找
- 在winform窗口上,实现按回车键(Enter)光标移至下一个控件的方法
- 根据控件名称查找控件
- 我的公共子字符串查找方法,非递归实现或者可以说没有实现递归
- 递归4: 折半查找的递归实现
- 递归实现单链表的查找
- 二分查找的递归实现
- 折半查找的递归实现
- Unity根据名称递归查找一个物体的子物体
- winform实现combox控件自动匹配查找,模糊查询功能
- 数据结构之二叉查找树的递归和非递归实现的一些方法
- Java实现分叉查找--递归方法
- 递归方法实现二分查找法
- Hive性能调校
- TestComplete总结八
- Linux高端内存的由来
- MongoDB官方文档翻译系列之 -- 执行二阶段提交
- hive优化
- 实现按名称递归查找控件的方法WinForm
- TestComplete总结九
- 二维动态
- JSR303,格式为##.00并且范围为15
- mtk6575
- Linux系统新手学习的11点建议
- 插入排序
- Neutron印象7:VPNaas
- Ubuntu下从ftp服务器获取文件的方法