C# – Dragging and dropping controls onto a custom user control become hidden

cdesign-timedrag and dropuser-controlswinforms

I created a custom UserControl, where I support dragging and dropping of controls at design time. My controls are dropped into my user control correctly, however they are hidden once dropped onto the user control. To get the added control to become visible, I have to select it and click the design time IDE button "Bring to Front" to see the control added. When I rebuild the solution, the controls become hidden again.

I reproduced the issue with the following code. In the IDE I created a simple user control "MyControl", to which I added a single Panel control docked to "Fill". This user control "MyControl" should then be dropped onto a Windows panel. Then, drag and drop another control, such as a Label or Buton control onto the user control and it becomes hidden.

Below is the code for the user control. How can I get the controls that are dropped into the user control at design time be brought to the front automatically?

MyControl.Designer.cs:

namespace Test
{
    partial class MyControl
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(150, 150);
            this.panel1.TabIndex = 0;
            // 
            // MyControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.panel1);
            this.Name = "MyControl";
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.Panel panel1;
    }
}

MyControl.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Axiom.Controls
{
    [Designer(typeof(ParentControlDesigner))]
    public partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel ContentPanel
        {
            get
            {
                return this.panel1;
            }
        }
    }

    internal class MyControlDesigner : ControlDesigner
    {
        public override void Initialize(IComponent component)
        {

            base.Initialize(component);

            MyControl control = component as MyControl;

            EnableDesignMode(control.ContentPanel, "ContentPanel");
        }
    }
}

Update:

I found another related question/answer to this problem of dragging and dropping controls at design time onto a container control within the custom user control:

  1. Question
  2. Referenced Article

I followed the above article and successfully dragged and dropped toolbox controls onto a container control within my custom user control at design time.

Best Answer

The problem is really pretty simple. You say that you've created a UserControl, then added a Panel control to it that is docked to fill the entire UserControl.

Thus, whenever you add controls to the UserControl at design time, you're not adding them to the Panel control, but instead the UserControl itself. That causes them to be covered up by the Panel control that fills the entire UserControl.

Bringing them to the front is a temporary solution, as that places them on top of the Panel control. It does not, however, place them inside the Panel control, which is why they "disappear" again when you rebuild the project. They don't actually disappear, they just get hidden by the Panel control again, as the default Z order arrangement has them located underneath the Panel.

It's not clear why you need the Panel control at all. If it fills the entire UserControl, it doesn't seem to serve any purpose. A UserControl is already a container control, so you don't need a Panel. Try removing it from your UserControl, and then you can add whatever other controls you want at design time without them being covered up by the docked Panel.

If you absolutely have to have a Panel control filling your UserControl, you need to add a line of code that automatically adds the control being dropped onto the UserControl at design time to the Controls collection for the Panel control.

Related Topic