如何让复合控件的子控件获得设计时支持

 假如一个复合控件由一个ToolStript和一个TextBox组成,如何使ToolStript获得设计时支持, 在设计时可以像使用普通ToolStript一样添加/删除控件?

      直接把ToolStript设为Public是无法达到目的的。必须定义一个用于为组件实现设计时服务的Designer

[Designer(typeof(MyDesigner))]

    public partial class UserControl1 : UserControl

 


      MyDesigner继承自System.Windows.Forms.Design. ControlDesigner 类, 它提供了一个方法“EnableDesignMode”。使用它将启用子控件的设计时功能,但需要将子控件公开为主控件的属性。

     全部代码如下:
   
如何让复合控件的子控件获得设计时支持
[Designer(typeof(MyDesigner))]

    public partial class UserControl1 : UserControl

    {

        public UserControl1()

        {

            InitializeComponent();

        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        public ToolStrip MyToolStrip

        {

            get { return this.toolStrip1 ; }

        }



    }



internal class MyDesigner : ControlDesigner

    {

        private UserControl1 MyControl;



        public override void Initialize(IComponent component)

        {





            base.Initialize(component);



            // Record instance of control we're designing

            MyControl = (UserControl1)component;

            this.EnableDesignMode(MyControl.MyToolStrip, "MyToolStrip");

        }

    }

 

 

 
 
 

你可能感兴趣的:(设计)