Deleting a user control causes a "denenv.exe - System error" error

Viewed 58

There is a User Control of type Panel with property "BindingContainer", which places the UC in the desired container.

When trying to remove ONLY UC - no problem. But if you set the "BindingContainer" property using the "Properties window" and try to delete (a container with an element inside it), a "denenv.exe - System error" - Failed to create a new protection page for the stack. Visual Studio closes.

-- UC TransparentPanel --

  public class TransparentPanel : Panel {
    protected override CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
        }
    }
    
    protected override void OnPaintBackground(PaintEventArgs e) {
        //base.OnPaintBackground(e);
    }

    protected override void OnCreateControl() {
        base.OnCreateControl();

        if (!DesignMode && BindingContainer != null) {
            this.Size = new Size(BindingContainer.Width - 20, BindingContainer.Height);
            this.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

            this.BringToFront();
        }
    }

    public new bool DesignMode { get; } =
        (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");

    private Control bindingContainer;
    public Control BindingContainer {
        get { return bindingContainer; }
        set {
            bindingContainer = value;
            BindingContainerChanged();
        }
    }

    private void BindingContainerChanged() {
        if (BindingContainer == null) return;

        BindingContainer.Controls.Add(this);

        this.Location = new Point(0, 0);
        this.Size = new Size(15, BindingContainer.Height);
        this.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
    }
  }
1 Answers

Code that works without problems.

public class TransparentPanel : Panel {
    public TransparentPanel() {
        this.Size = new Size(110, 55);

        SetStyle(ControlStyles.Opaque |
            ControlStyles.ResizeRedraw, true);

        UpdateStyles();
    }

    protected override CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
        }
    }

    protected override void OnCreateControl() {
        if (!IsDesignMode(this)) {
            this.Location = new Point(0, 0);
            this.BorderStyle = BorderStyle.FixedSingle;
            this.Size = new Size(BindingContainer.Width - 20, BindingContainer.Height);
            this.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

            this.BringToFront();
        }
    }

    protected override void OnParentChanged(EventArgs e) {
        BindingContainer = this.Parent;
    }

    public Control BindingContainer { get; private set; }

    // Can be moved to a separate class
    public bool IsDesignMode(IComponent component) {
        bool ret = false;

        if (null != component) {
            ISite site = component.Site;

            if (null != site) {
                ret = site.DesignMode;
            } else if (component is Control) {
                IComponent parent = (component as Control).Parent;
                ret = IsDesignMode(parent);
            }
        }

        return ret;
    }
}
Related