728x90
728x170

사용자 컨트롤 작성시 사용자 컨트롤에 다른 컨트롤들을

배치 시킬수 있도록 하기 위한 방법입니다.

 

UserControl (TestControl) 을 만들고 사용자가 편집 가능하게 할 컨트롤을 아래 처럼 정의합니다.

 

namespace Test

{

    public partial class TestControl : UserControl

    {

        /// <summary>

        /// 사용자 정의 판넬입니다.

        /// </summary>

        [Category("Test")]

        [Browsable(false)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        public Panel UserPanel => this.userPanel;

    }

}

 

ParentControlDesigner 를 상속받는 디자이너 클래스를 추가합니다.

 

using System.ComponentModel;

using System.Windows.Forms.Design;

 

namespace Test

{

   public class TestControlDesigner : ParentControlDesigner

    {

        public override void Initialize(IComponent component)

        {

            base.Initialize(component);

 

            TestControl testControl = component as TestControl;

            if (dsGridSelector != null)

            {

                base.EnableDesignMode(dsGridSelector.UserPanel, "UserPanel");

            }

        }

    }

}

 

UserControl 의 상단에 아래 처럼 처리합니다.

 

namespace Test

{

    [Designer(typeof(TestControlDesigner))]

    public partial class TestControl : UserControl

    {

        /// <summary>

        /// 사용자 정의 판넬입니다.

        /// </summary>

        [Category("Test")]

        [Browsable(false)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        public Panel UserPanel => this.userPanel;

    }

}

 

이게 디자이너 단에서 UserPanel 부분으로 다른 컨트롤 들을 배치 시킬수 있습니다.

 

만약 Panel 안의 Panel 을 처리하고 싶을때는 부모 판넬도 위와 같은 처리를 해야

정상적으로 동작합니다.

 

예시)

Panel1 안에 Panel2 가 있는 경우 Panel2 로 디자이너에서 편집이 가능하게 할 경우

 

namespace Test

{

    [Designer(typeof(TestControlDesigner))]

    public partial class TestControl : UserControl

    {

        /// <summary>

        /// 사용자 정의 판넬1입니다.

        /// </summary>

        [Category("Test")]

        [Browsable(false)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        public Panel UserPanel1 => this.userPanel1;

 

        /// <summary>

        /// 사용자 정의 판넬2입니다.

        /// </summary>

        [Category("Test")]

        [Browsable(false)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        public Panel UserPanel2 => this.userPanel2;

 

    }

}

 

 

using System.ComponentModel;

using System.Windows.Forms.Design;

namespace Test

{

    public class TestControlDesigner : ParentControlDesigner

    {

        public override void Initialize(IComponent component)

        {

            base.Initialize(component);

 

            TestControl testControl = component as TestControl;

            if (dsGridSelector != null)

            {

                base.EnableDesignMode(dsGridSelector.UserPanel1, "UserPanel1");

                base.EnableDesignMode(dsGridSelector.UserPanel2, "UserPanel2");

            }

        }

    }

}

 

 

 

 

 

 

728x90
그리드형
Posted by kjun
,