Get all controls of the current form at design-time

前端 未结 4 727
陌清茗
陌清茗 2021-01-25 14:43

I have a question about design-time things:

I\'ve made a component with an property \"Links\". Those links are Controls. Now I want to make a UI-Dialog (for editing this

4条回答
  •  梦谈多话
    2021-01-25 15:18

    You can get IDesignerHost service at design-time. This service has a property called Container which has Components. Then for each component, get INestedContainer service and then get all components from that service.

    This is how Document Outline window works. I've changed their method to use List as return value:

    List GetSelectableComponents(IDesignerHost host)
    {
        var components = host.Container.Components;
        var list = new List();
        foreach (IComponent c in components)
            list.Add(c);
        for (var i = 0; i < list.Count; ++i)
        {
            var component1 = list[i];
            if (component1.Site != null)
            {
                var service = (INestedContainer)component1.Site.GetService(
                    typeof(INestedContainer));
                if (service != null && service.Components.Count > 0)
                {
                    foreach (IComponent component2 in service.Components)
                    {
                        if (!list.Contains(component2))
                            list.Add(component2);
                    }
                }
            }
        }
        return list;
    }
    

    To filter the result to contain just controls, you can call result.TypeOf().

提交回复
热议问题