zoukankan      html  css  js  c++  java
  • 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(thisnew SearchHandler(mehtod));
                
    if (btn != null)
                
    {
                    MessageBox.Show(btn.Text);
                }

            }

            
    public bool mehtod(Control ctr)
            
    {
                
    if (ctr.Text =="button2")
                    
    return true;
                
    return false;
            }

     


    作者:破  狼
    出处:http://www.cnblogs.com/whitewolf/
    本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼

  • 相关阅读:
    两个 Gadget 小程序
    Microsoft Visual Studio Team System 2008 中的本地负载测试
    Silverlight 2.0细节
    用后台代码创建Storyboard
    DoubleAnimation方法
    Silverlight Random class is not very random
    微软Silverlight移动版本将于年内推出 支持S60
    yahoo也有了Silverlight Developer Center
    微软证实新版Silverlight将具备离线应用功能
    xaml设计实验
  • 原文地址:https://www.cnblogs.com/whitewolf/p/1606587.html
Copyright © 2011-2022 走看看