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:
I followed the above article and successfully dragged and dropped toolbox controls onto a container control within my custom user control at design time.