Answered by:
UserControl on Form and other UserControl

Question
-
Hi,
I have a custom control that holds a list of controls in the Controls-collection. I want to display one control of these dynamically on a form. So I open the form and add the control to the Controls-collection of the form.
Problem: When the form closes, the control on the form is disposed and removed from the Controls-Collection of my custom control.
How can I reuse a control so that the same instance can be held in my custom control and displayed dynamically on a form that pops up when the user presses a button. I tried to change the parent but the controls is disposed lso when closing the form.
Thanx and kind regards
Matthias
Matthias (CTO PFGrid) winforms.pfgrid.comSaturday, August 14, 2010 2:36 PM
Answers
-
You cannot. That behavior is by design. A control can belong to only one Controls collection at a time. If you could, what value should the Control.Parent property be? It is not an array. It should have been removed from the custom control as soon as you added it to a new collection. Test and verify. Controls are not serializable either.
I have not tried this. You might try removing the control from the one collection and putting it into another, though. But, it is a practice that I would not recommend. You will run into issues with event delegates associated with the control, which would cause stuff to hang around in memory and not get disposed in a timely fashion.
Mark the best replies as answers. "Fooling computers since 1971."- Marked as answer by Jing0 Wednesday, August 18, 2010 8:09 PM
Saturday, August 14, 2010 6:11 PM -
Like asking how can I be in two places at the same time. Takes two instances or a magician.
- Marked as answer by Jing0 Wednesday, August 18, 2010 8:09 PM
Saturday, August 14, 2010 6:50 PM -
As the others have said, exactly what you ask is impossible. Perhaps you asked the question though, thinking this was a way to solve some requirement, and the requirement is what you should state. Others may have had the same requirement as you and can suggest an alternative.
--
Mike- Marked as answer by Jing0 Wednesday, August 18, 2010 8:09 PM
Saturday, August 14, 2010 6:55 PM
All replies
-
You cannot. That behavior is by design. A control can belong to only one Controls collection at a time. If you could, what value should the Control.Parent property be? It is not an array. It should have been removed from the custom control as soon as you added it to a new collection. Test and verify. Controls are not serializable either.
I have not tried this. You might try removing the control from the one collection and putting it into another, though. But, it is a practice that I would not recommend. You will run into issues with event delegates associated with the control, which would cause stuff to hang around in memory and not get disposed in a timely fashion.
Mark the best replies as answers. "Fooling computers since 1971."- Marked as answer by Jing0 Wednesday, August 18, 2010 8:09 PM
Saturday, August 14, 2010 6:11 PM -
Like asking how can I be in two places at the same time. Takes two instances or a magician.
- Marked as answer by Jing0 Wednesday, August 18, 2010 8:09 PM
Saturday, August 14, 2010 6:50 PM -
As the others have said, exactly what you ask is impossible. Perhaps you asked the question though, thinking this was a way to solve some requirement, and the requirement is what you should state. Others may have had the same requirement as you and can suggest an alternative.
--
Mike- Marked as answer by Jing0 Wednesday, August 18, 2010 8:09 PM
Saturday, August 14, 2010 6:55 PM