Answered by:
Add Control to UserControl

Question
-
I have a UserControl and use it on Window Form
the problem is when i add any control like "label" to my usercontrol the label goes out of my usercontrol not inside itwhat can i do to let my usercontrol to accept any other control in side it?
Tuesday, November 6, 2007 7:05 PM
Answers
-
You can add a Panel into the UserControl and dock it to fill in the UserControl;then create a custom ControlDesigner class to call the EnableDesignMode method to enable dragging controls to the panel at design time, the following sample demonstrates how to do this:
Code Block
[Designer(typeof(myControlDesigner))]public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.innerPanel = new Panel();
this.innerPanel.Dock = DockStyle.Fill;
this.Controls.Add(this.innerPanel);
}
private Panel innerPanel;
public Panel InnerPanel
{
get { return innerPanel; }
set { innerPanel = value; }
}
}
class myControlDesigner : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
UserControl1 uc = component as UserControl1;
this.EnableDesignMode(uc.InnerPanel, "Panel");
}
}
Friday, November 9, 2007 9:51 AM
All replies
-
You can add a Panel into the UserControl and dock it to fill in the UserControl;then create a custom ControlDesigner class to call the EnableDesignMode method to enable dragging controls to the panel at design time, the following sample demonstrates how to do this:
Code Block
[Designer(typeof(myControlDesigner))]public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.innerPanel = new Panel();
this.innerPanel.Dock = DockStyle.Fill;
this.Controls.Add(this.innerPanel);
}
private Panel innerPanel;
public Panel InnerPanel
{
get { return innerPanel; }
set { innerPanel = value; }
}
}
class myControlDesigner : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
UserControl1 uc = component as UserControl1;
this.EnableDesignMode(uc.InnerPanel, "Panel");
}
}
Friday, November 9, 2007 9:51 AM -
I found the solution, just add this in the top of the code (tested in vb.net 2005)
Imports
System.ComponentModelImports
System.ComponentModel.Design<Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
Public
Class .....your code .....
End
ClassFriday, November 16, 2007 10:18 AM -
I heve tried the code posted above and shows that the controls are actually added to the panel container, but i cant see them at run time. Any idea of what could be happening.Tuesday, June 16, 2009 10:45 PM
-
I solved it this way....very similar to code shown above, just with a little modifications.
[Designer(typeof(myControlDesigner))]
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.innerPanel = new Panel();
this.innerPanel.Dock = DockStyle.Fill;
this.Controls.Add(this.innerPanel);
}
private Panel innerPanel;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel InnerPanel
{
get { return innerPanel; }
set { innerPanel = value; }
}
}
class myControlDesigner : ParentControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
UserControl1 uc = component as UserControl1;
this.EnableDesignMode(uc.InnerPanel, "InnerPanel");
}
}
Wednesday, June 17, 2009 2:06 AM -
I have tried the recommended solutions above. I am able to drag controls onto the panel. But, the panel seems to be painting over the controls. If I select the custom panel and drag/move it, I can see the added controls. But once I stop dragging, it goes behind the panel again. At runtime, I cannot see the added controls. What am I doing wrong?Wednesday, September 23, 2009 8:55 PM
-
I have tried the recommended solutions above. I am able to drag controls onto the panel. But, the panel seems to be painting over the controls. If I select the custom panel and drag/move it, I can see the added controls. But once I stop dragging, it goes behind the panel again. At runtime, I cannot see the added controls. What am I doing wrong?
Sorry, this answer comes with huge delay, but maybe some other poor soul has the same problem. Apparently, VS does not automatically add the " add your control to the panel " command in the generated code. So, in the ...Designer.cs you have to modify the following lines (examples):this.myControlDesigner1.Name = "myControlDesigner"; this.myControlDesigner1.InnerPanel = this.myControlDesigner1.InnerPanel; this.myControlDesigner1.InnerPanel.Controls.Add(this.myNewButton); // This line has to be added manually!
Cheers
- spitfire
Monday, January 3, 2011 11:01 PM