WinForm自定义函数FindControl实现按名称查找控件
作者:shichen2014 时间:2021-09-23 17:27:25
本文所述实例实现WinForm自定义函数FindControl实现按名称查找控件的功能,在C#程序开发中有一定的实用价值。分享给大家供大家参考。
关键代码如下:
/// <summary>
/// 按名称查找控件
/// </summary>
/// <param name="parentControl">查找控件的父容器控件</param>
/// <param name="findCtrlName">查找控件名称</param>
/// <returns>若没有查找到返回NULL</returns>
public static Control FindControl(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;
}
}
}
return _findedControl;
}
/// <summary>
/// 将Control转换某种控件类型
/// </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 = panel1.FindControl("checkBox1").Cast<CheckBox>(out _sucess);
if (_sucess)
{
MessageBox.Show(_finded.Name);
}
else
{
MessageBox.Show("Not Finded.");
}
希望本文实例对大家C#学习能有所帮助!
标签:WinForm,查找,控件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用NPOI导入Excel的方法详解
2021-08-29 07:39:35
Java流程控制之选择结构
2021-10-24 08:12:50
![](https://img.aspxhome.com/file/2023/4/67464_0s.jpg)
一篇文章教你如何用多种迭代写法实现二叉树遍历
2023-12-23 04:03:29
java网络爬虫连接超时解决实例代码
2022-02-02 06:57:28
![](https://img.aspxhome.com/file/2023/2/85422_0s.png)
深入分析Java异常
2022-10-29 16:06:40
c# for循环中创建线程执行问题
2021-09-15 20:43:23
![](https://img.aspxhome.com/file/2023/3/108703_0s.png)
Qt之调用C#的动态库的解决方法
2023-07-22 03:42:45
![](https://img.aspxhome.com/file/2023/1/67061_0s.png)
Spring Boot中操作使用Redis实现详解
2023-11-24 02:39:29
![](https://img.aspxhome.com/file/2023/9/85399_0s.png)
C#图像伪彩色处理方法
2022-09-23 10:51:52
![](https://img.aspxhome.com/file/2023/1/92911_0s.jpg)
使用IDEA异常断点来定位java.lang.ArrayStoreException的问题
2022-06-14 00:43:18
![](https://img.aspxhome.com/file/2023/9/75499_0s.png)
Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件
2023-07-06 01:24:29
![](https://img.aspxhome.com/file/2023/9/120089_0s.webp)
使Java的JButton文字隐藏功能的实现(不隐藏按钮的前提)
2022-06-01 23:54:33
![](https://img.aspxhome.com/file/2023/0/83130_0s.gif)
Java控制台实现猜拳游戏
2022-12-15 09:54:46
java组件commons-fileupload文件上传示例
2022-08-16 02:42:56
![](https://img.aspxhome.com/file/2023/2/97792_0s.jpg)
Android实现拍照、选择相册图片并裁剪功能
2022-09-22 12:25:44
mybatis-plus 如何判断参数是否为空并作为查询条件
2022-09-14 22:05:03
![](https://img.aspxhome.com/file/2023/8/61078_0s.jpg)
WPF弹出自定义窗口的方法
2023-10-18 20:52:04
Java 实现网络爬虫框架详细代码
2021-12-11 05:15:43
Java语言求解完美数代码分析
2023-01-28 10:17:58
![](https://img.aspxhome.com/file/2023/1/64791_0s.png)
Java Lambda表达式实例解析原理
2022-11-02 08:11:02