UserControl extends ScrollableControl - disables container functionality

I create user control by expanding ScrollableControl .
The problem is that my user control acts like a container - I can drag and drop controls into it: enter image description here

My question is how can I disable container functionality in a class that extends ScrollableControl

Two control elements are listed below: one extends Control , the second 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); } } 
+5
source share
2 answers

During development, from the attribute [Designer] you get the behavior "act-like-a-container". Copy paste from Help source :

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

This ScrollableControlDesigner does the job. This by itself does not, but comes from the ParentControlDesigner, a constructor that allows the control to act as the parent for the child controls and gives it container-like behavior during development.

Easy to fix, you just need to use your own [Designer] attribute to select another designer. Add a link to System.Design and do this:

 using System.Windows.Forms; using System.Drawing; using System.ComponentModel; using System.Windows.Forms.Design; // Add reference to System.Design [Designer(typeof(ControlDesigner))] public class ScrollableControlBasedControl : ScrollableControl { // etc... } 
+1
source

There are probably several ways to do this, but here is what I will do ...

First create a version of ControlCollection .

 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"); } } 

Then create a ScrollableControlBasedControl instance of ReadOnlyControlCollection instead of the standard ControlCollection

 public class ScrollableControlBasedControl : ScrollableControl { protected override Control.ControlCollection CreateControlsInstance() { return new ReadOnlyControlCollection(this); } // The rest of your class goes here... } 

I am using Visual Studio 2010, and when I drop the control onto a ScrollableControlBasedControl , the control magically moves back to where it came from, as if the action was canceled.

0
source

Source: https://habr.com/ru/post/1266357/


All Articles