Windows Develop Bookmark and Share   
 index > Windows Forms General > 'child' is not a child control of this parent
 

'child' is not a child control of this parent

I have a UserControl that has in it a TableLayoutPanel. I activate ControlAdded event that adds the controls added in the UserControl to the TableLayoutPanel:

private void UserControl1_ControlAdded(object sender, ControlEventArgs e)
{
tableLayoutPanel1.Controls.Add(e.Control);
}

The problem is that at DesignTime when I'm trying to add a control to my new UserControl, the VisualStudio 2005 crashes.
If I have another UserCOntrol that inherits from it i get this error: 'child' is not a child control of this parent

Can anyone give me some sample code that solves this problem?

Thanks


Cornel Gav  Thursday, April 24, 2008 3:00 PM
Cornel Gav wrote:
I have a UserControl that has in it a TableLayoutPanel. I activate ControlAdded event that adds the controls added in the UserControl to the TableLayoutPanel:

Code Snippet
private void UserControl1_ControlAdded(object sender, ControlEventArgs e)
{
tableLayoutPanel1.Controls.Add(e.Control);
}


The problem is that at DesignTime when I'm trying to add a control to my new UserControl, the VisualStudio 2005 crashes.
If I have another UserCOntrol that inherits from it i get this error: 'child' is not a child control of this parent

Can anyone give me some sample code that solves this problem?

Thanks


That event you postedis firing off when you add the controls. I'd bet oneobject or the other could be null. Try setting a breakpoint in that method.

Rudedog

Rudedog2  Thursday, April 24, 2008 4:46 PM
Cornel Gav wrote:
I have a UserControl that has in it a TableLayoutPanel. I activate ControlAdded event that adds the controls added in the UserControl to the TableLayoutPanel:

Code Snippet
private void UserControl1_ControlAdded(object sender, ControlEventArgs e)
{
tableLayoutPanel1.Controls.Add(e.Control);
}


The problem is that at DesignTime when I'm trying to add a control to my new UserControl, the VisualStudio 2005 crashes.
If I have another UserCOntrol that inherits from it i get this error: 'child' is not a child control of this parent

Can anyone give me some sample code that solves this problem?

Thanks


That event you postedis firing off when you add the controls. I'd bet oneobject or the other could be null. Try setting a breakpoint in that method.

Rudedog

Rudedog2  Thursday, April 24, 2008 4:46 PM

Does it work as you expect at runtime? I would imagine not, but based on what you've said it appears I might be wrong.

If you are trying to prevent a control from being added to the form, and instead have it added to the TableLayoutPanel, you should override the OnControlAdded event, rather than putting this code in an event handler.

If, for whatever reason you need to stick with the design you've shown, you could try to wrap the tableLayoutPanel1.Controls.Add(e.Control); line in an If(Designmode = True) or If(Debugger.Attached = True) clause.

JayStation3  Friday, April 25, 2008 7:05 PM
It's not too surprising you crash the designer with this code. It is trying to add the control to the UserControl but then you immediately jank it out again when you make the TableLayoutPanel the parent of the control. I'm guessing tableLayoutPanel1 is a private member of the UC rather part of the UC's Controls collection. If you do it the latter way, the designer will drop the control directly into the TLP instead of the UC and avoid the problem. Be sure to make the TLP large enough to fill the UC (dock fill it).
nobugz  Saturday, April 26, 2008 7:57 PM

You can use google to search for other answers

Custom Search

More Threads

• Is it possible to drag and drop an item from outlook mail/desktop to a windows form
• How to Trap Exact Error Line....
• Different toolTip for listBox items
• Data Binding again
• Determining if text going to control is unicode
• Display tool tip for each dropdownlist Items
• Docking in Custom Control
• DataGridView Bug or my mistake
• Load form partially
• SelectNextControl backwards in nested user controls