WinForm控件查找奇思
最近做WinForm程序,尽搞些动态生成控件的,每次寻找某个空间时总要一大堆代码,简单但是写的多,烦啊。突然想起了Linq里的表达式方式,但是项目用的类库是2.0的。最后仿照Linq用范型写了一个遍历类:减少了一大堆不必要的代码。
代码很简单,就不用解释了,直接贴↑。
- public delegate bool SearchHandler(Control ctrFind);
- public class WinSearch<T>
- {
- //ctr:查找起点控件。
- public static T Search(Control ctr, SearchHandler handler)
- {
- if (handler == null)
- throw new Exception("Handler must be not null");
- if (ctr == null)
- throw new Exception("Parent Control must be not null");
- if (!(ctr is Control))
- throw new Exception("The fist parameter must be innert from Control");
- return SearchProxy(ctr, handler);
- }
- protected static T SearchProxy(Control ctr,SearchHandler handler)
- {
- if (ctr.Controls.Count < 1)
- {
- return default(T);
- }
- else
- {
- foreach (Control child in ctr.Controls)
- {
- if (child is T && handler(child))//注意返回范型类型应是如此才会返回。
- {
- return (T)(object)child;
- }
- else
- {
- foreach (Control ch in child.Controls)
- {
- object obj = SearchProxy(ch, handler);
- if (obj !=null)
- {
- return (T)obj;
- }
- }
- }
- }
- return default(T);
- }
- }
- }
- 测试体:
- private void button1_Click(object sender, EventArgs e)
- {
- Button btn = WinSearch<Button>.Search(this, new SearchHandler(mehtod));
- if (btn != null)
- {
- MessageBox.Show(btn.Text);
- }
- }
- public bool mehtod(Control ctr)
- {
- if (ctr.Text =="button2")
- return true;
- return false;
- }
WinForm控件查找奇思相关推荐
- c# 扩展方法奇思妙用高级篇六:WinForm 控件选择器
在Web开发中,jQuery提供了功能异常强大的$选择器来帮助我们获取页面上的对象.但在WinForm中,.Net似乎没有这样一个使用起来比较方便的选择器.好在我们有扩展方法,可以很方便的打造一个. ...
- 如何设置Winform控件的ClientRectangle
最近学习制作WinForm控件,自己动手写控件的时候才发现System.Windows.Forms.Control 竟然没有提供默认的border绘制.记得以前用API做控件的时候,只需要设置空间窗口 ...
- c# 开发winform控件
c#开发winform控件 工作中常常遇到这样的问题,为了达到某种特殊的需求,我们常要定制一些控件.写控件让人头大的一件事,开始写控件总是很难的.不过有人已经给我们开了一个好头,跟着学习一下: 转链接 ...
- 基于C#的GIS开发20210426之类库同名类、制作winform控件、建立控件之间的关联、自己制作控件、自己控件引入项目、显示磁盘文件夹、磁盘文件与picturebox建立关系、图像拉伸
C#知识点与控件的制作 一.C#中多个类库中有同名类,区分问题 二.制作最简单的winform控件 1.拖动加载 事件代理 筛选器 2.显示属性.事件,及其修改 常见控件属性介绍 窗体常用的方法 常见 ...
- (转)WinForm控件使用文章收藏整理完成
http://home.cnblogs.com/group/topic/29829.html 对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. ...
- 异步委托实现多线程winform控件编程
private void button1_Click(object sender, EventArgs e) { ThreadStart ts ...
- C# WinForm控件、自定义控件整理(大全)
C# WinForm开发系列 - CheckBox/Button/Label/ProgressBar WinForm下CheckedListBox的数据绑定 Winform 下无闪烁走马灯效果实现 c ...
- 在WPF中使用WinForm控件方法
在WPF中使用WinForm控件方法 原文:在WPF中使用WinForm控件方法 1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,Syste ...
- Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决
Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决 参考文章: (1)Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决 (2)https://www. ...
最新文章
- 这些年,还是一个人~
- maven开发mybatis 让*.xml 拷贝到classes目录下
- 互联网算法面试高频题目
- 「Apollo」百度Apollo感知模块(perception)红绿灯检测代码完整+详细解析
- 51nod 1278 相离的圆
- java系统项目分为哪五大层次?控制层_业务_一个项目中说系统分为表现层、控制层、逻辑层、DAO层和最终数据库五层架构-转...
- 斐波那契数列的Python实现
- 认识Hibernate
- 关于STM32L100xx, STM32L151xx, STM32L152xx ,STM32L162xx 的Power Control
- 常见的文件后缀名大全
- cwRsync实现windows server文件定时自动同步
- 由膳食纤维选择性促进的肠道细菌缓解二型糖尿病
- Cookie中path总结
- IP地址冲突解决方案,局域网IP地址冲突如何解决?
- ChainZ Arena攻略+全红卡评分
- jekins自动化部署
- 模拟开关和数字开关的区别
- 给文字做涂鸦效果的五种实现方案
- linux系统编程之网络编程
- C++使用switch动态获取函数(通过函数指针实现)