Windows Develop Bookmark and Share   
 index > Windows Forms General > Add Control to UserControl
 

Add Control to UserControl

Ihave aUserControlanduseitonWindowForm
the problem is when i add any control like "label" to my usercontrol the label goes out of my usercontrol not inside it

what can i do to let my usercontrol to accept any other control in side it?

haihtomy  Tuesday, November 06, 2007 7:05 PM

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");

}

}

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");

}

}

I found the solution, just add this in the top of the code (tested in vb.net 2005)

Imports System.ComponentModel

Imports System.ComponentModel.Design

<Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _

Public Class .....

your code .....

End Class

haihtomy  Friday, 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.
hajrodriguez  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))]

publicpartialclassUserControl1:UserControl

publicUserControl1()

this.innerPanel =newPanel();

this.innerPanel.Dock =DockStyle.Fill;

this.Controls.Add(this.innerPanel);

privatePanelinnerPanel;


[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

publicPanelInnerPanel

get{returninnerPanel; }

set{ innerPanel =value; }

classmyControlDesigner:ParentControlDesigner

publicoverridevoidInitialize(IComponentcomponent)

base.Initialize(component);

UserControl1uc = componentasUserControl1;

this.EnableDesignMode(uc.InnerPanel,"InnerPanel");

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?

bheimsoth  Wednesday, September 23, 2009 8:55 PM

You can use google to search for other answers

Custom Search

More Threads

• DisplayMember is not displayed
• Renaming an existing file
• removing unwanted event methods without editing "#region Component Designer generated code" manually
• How to make the end of TextBox.Text in view?
• Customized File Dialog Box
• Calender Contorl bug ?
• How to prevent MDI Child Forms from Showing Menu
• RichTextBox Text Selection
• Force ComboBox userChange event
• Error:Type not marked as serializable