Showing posts with label Reflection. Show all posts
Showing posts with label Reflection. Show all posts

Sunday, April 26, 2009

Reflection

This code is used to change all off  one kind of controls one property to any value in a form.Hard to understand ? Ok let me give you an ex: you want to disable all text box in a form property is enabled value is false very easy.

private static ArrayList controls = new ArrayList();
    public static Control[] LookControls(Form f,Type ctrlType,string property,object value)
    {
  
        foreach (Control c in f.Controls)
        {
            LookControls(c, ctrlType,property,value);
        }
        return (Control[])controls.ToArray(typeof(Control));
    }
    private static void LookControls(Control ctrl, Type ctrlType,string property,object value)
    {
        if (ctrl.GetType() == ctrlType)
        {
            controls.Add(ctrl);
            PropertyInfo p = ctrl.GetType().GetProperty(property);
            p.SetValue(ctrl, value, null);
        }
       
        if (ctrl.Controls != null)
        {
            foreach (Control ctrl1 in ctrl.Controls)
            {
                LookControls(ctrl1,ctrlType,property,value);
            }
        }
    }