Find components on a windows form c# (not controls)

后端 未结 2 940
攒了一身酷
攒了一身酷 2020-12-17 14:53

I know how to find and collect a list of all the controls used in a Windows Form. Something like this:

static public void FillControls(Control control, List         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 15:53

    Surprisingly, it seems the only way to do this is via reflection.

    private IEnumerable EnumerateComponents()
    {
        return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
               where typeof (Component).IsAssignableFrom(field.FieldType)
               let component = (Component) field.GetValue(this)
               where component != null
               select component;
    }
    

提交回复
热议问题