2017-04-05 4 views
5

ScrollableControl을 확장하여 사용자 정의 컨트롤을 작성하고 있습니다.
문제는 컨테이너로 그 내 사용자 지정 컨트롤 행위이다 - 나는 그것으로 컨트롤을 드래그 할 수 있습니다 : 이 enter image description hereUserControl extends ScrollableControl - 컨테이너 기능을 비활성화하십시오.

내 질문은 내가 ScrollableControl

아래는 두 개의 테스트 컨트롤, 하나 확장 클래스에서 컨테이너 기능을 해제 할 수있는 방법입니다 둘째, Control 확장 ScrollableControl이 이러한 목표를 달성하기 위해 하나 이상의 방법은 아마,하지만 여기에 내가 무엇을 할 것이라고입니다

public class ControlBasedControl : Control 
{ 
    protected override Size DefaultSize 
    { 
     get { return new Size(100, 100); } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     e.Graphics.FillRectangle(Brushes.LightCoral, ClientRectangle); 
    } 
} 

public class ScrollableControlBasedControl : ScrollableControl 
{ 
    public ScrollableControlBasedControl() 
    { 
     AutoScrollMinSize = new Size(200, 200); 
    } 

    protected override Size DefaultSize 
    { 
     get { return new Size(100, 100); } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     e.Graphics.FillRectangle(Brushes.LawnGreen, ClientRectangle); 
    } 
} 
+0

아마도 Drop 이벤트를 컨테이너 속성이 null이거나 대상 컨트롤에서 자식을 삭제하려고 할 수 있습니다. –

+0

@BaranovskiyDmitry 디자인 타임으로 비활성화하려고합니다. 놓기 이벤트는 런타임에 작동합니다. – Misiu

답변

1

디자인 타임에 [디자이너] 특성에서 "행위와 같은 개념"의 동작이 발생합니다. Reference Source 복사 - 붙여 넣기 :

[ 
ComVisible(true), 
ClassInterface(ClassInterfaceType.AutoDispatch), 
Designer("System.Windows.Forms.Design.ScrollableControlDesigner, " + AssemblyRef.SystemDesign) 
] 
public class ScrollableControl : Control, IArrangedElement { 
    // etc... 
} 

그것은 일을 얻을 수 ScrollableControlDesigner입니다. 그 자체로는별로하지 않지만 컨트롤이 자식 컨트롤의 부모 역할을하도록 허용하고 디자인 타임에 컨테이너와 비슷한 동작을 제공하는 디자이너 인 ParentControlDesigner에서 파생됩니다.

해결 방법은 간단합니다. 디자이너의 고유 한 속성을 사용하여 다른 디자이너를 선택하기 만하면됩니다. System.Design에 대한 참조를 추가하고 다음과 같이 만듭니다.

0

...

먼저 그럼 난 비주얼 스튜디오 2010을 사용 ControlCollection

public class ScrollableControlBasedControl : ScrollableControl 
{ 
    protected override Control.ControlCollection CreateControlsInstance() 
    { 
     return new ReadOnlyControlCollection(this); 
    } 

    // The rest of your class goes here... 
} 

기본 대신에 ReadOnlyControlCollection의 인스턴스를 만들려면 ScrollableControlBasedControlControlCollection

public class ReadOnlyControlCollection : Control.ControlCollection 
{ 
    public ReadOnlyControlCollection(Control owner) 
     : base(owner) 
    { 
    } 

    public override bool IsReadOnly 
    { 
     get { return true; } 
    } 

    public override void Add(Control control) 
    { 
     throw new ArgumentException("control"); 
    } 
} 

의 읽기 전용 버전을 만들 6,

때 I 컨트롤을 드롭하면 ScrollableControlBasedControl 컨트롤은 마치 액션이 취소 된 것처럼 마침내 원래의 위치로 다시 이동합니다.