Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControl extending ScrollableControl - disable container functinality

I'm building custom control by extending ScrollableControl.
Problem is that my custom control acts as container - I can drag controls into it: enter image description here

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

Below are two test controls, one extends Control, 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);
    }
}
like image 655
Misiu Avatar asked Nov 07 '22 23:11

Misiu


1 Answers

You get "acts-like-a-container" behavior at design time from the [Designer] attribute. Copy-pasting from the Reference Source:

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

It is ScrollableControlDesigner that gets the job done. Doesn't do much by itself, but derived from ParentControlDesigner, the designer that permits a control to act as a parent for child controls and gives it container-like behavior at design time.

Fix is easy, you just have to use your own [Designer] attribute to select another designer. Add a reference to System.Design and make it look like 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...
}
like image 198
Hans Passant Avatar answered Nov 14 '22 23:11

Hans Passant