开发者

cannot loop oo components in a usercontrol

开发者 https://www.devze.com 2023-02-13 08:26 出处:网络
I am a bit confused.Iimplemented my own UserControl andI wantthat my controldiscover Components (like binding source) hosted in the same Control at design time.

I am a bit confused. I implemented my own UserControl and I want that my control discover Components (like binding source) hosted in the same Control at design time.

Code is like this:

private void FindComponentByName(string aName)  
{  
   foreach(Component component in this.Container.components)  
   {  
      if (Component.ToString()==aName)  
       {  
        dosomething();  
        break;   
        }  
   }  
}  

This code is not working either at design time or run time as Container is always nul开发者_JAVA技巧l.

If I run this code in a Form not in a UserControl

private Component FindComponentByName(string aname)  
        {  
            Component result = null;  
            foreach (Component component in this.components.Components)  
            {  
                if (component.ToString() == aname)  
                {  
                    result = component;  
                    break;  
                }  
            }  
            return result;  
        }

it works, as components is not null and I manage to retrieve all components.

I need to do that at run time and at design-time too.

Can someone please explain me what's my mistake? Thanks and regards paolo


Your code should be something like this:

private void FindComponentByName(string aName)  
{  
    Control c = FindComponentByName(this);
    if (c != null)
    {
        dosomething();
    }
}

private Control FindComponentByName(Control c, string aName)  
{
   if (c.Name == aName)
   {
       return c;
   }

   foreach(var control in c.Controls)  
   {  
      //recurse into containers controls to make sure we visit all depths
      Control found = ctrl.FindComponentByName(control, aName);
      if (found != null)
           return found;
   }
   return null;
}

You could even make that as an extension method so you can call that on whatever control you need it:

public static class MyExtensions
{
    public static Control FindControlWithName(this Control control, string aName)
    {
         if (control.Name == aName)
         {
            return control;
         }
         foreach(var ctrl in control.Controls)
         {
             Control found = ctrl.FindControlWithName(aName);
             if (found != null)
                 return found;
         }
         return null;
    }
}

and you can call it like:

if (someControl.FindControlWithName("hola") != null)
{
    dosomething();
}


Hello I found this solution which can be used by several parent class types (I present it as the extension defined for UserControl, but can be used for Form or Class).

public static class UserControlExtension
{
    public static IEnumerable<T> GetAllMembersByType<T>(this UserControl sourceObj, Type requiredType)
    {
        var members = sourceObj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        var found = members.Where(fi => fi.FieldType.Equals(requiredType));

        List<T> items = new List<T>();
        foreach (var fld in found)
        {
            T val = (T)fld.GetValue(sourceObj);
            items.Add(val);
        }

        return items;
    }

    public static T GetMemberByNameAndType<T>(this UserControl sourceObj, string requiredName, Type requiredType)
    {
        var members = sourceObj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        var found = members.Where(fi => fi.FieldType.Equals(requiredType) && fi.Name == requiredName);

        if (found.Any())
        {
            var fld = found.FirstOrDefault();
            T val = (T)fld.GetValue(sourceObj);
            return val;
        }

        throw new Exception("No member found.");
    }


}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号