Disappearing Nested User Controls in VS2005

When trying to nest usercontrols, the child usercontrol sometimes disappears off the parent usercontrol. This is a commonly reported bug on the internet, and there’s a hotfix FIX:#842706 for vs2003 but not for vs2005, and its extremely annoying. What happens is that after placing the MyControl on your MyForm.designer.cs, it just disappears when you save MyParentControl in wysiwyg mode.

For me, the problem occurs because the Component Designer auto-validates & re-generates your MyParentControl.designer.cs code again. And, if your MyControl constructor requires initializing parameters, vs2005 will just delete that instantiation in MyParentControl.designer.cs.

For example, if your MyControl’s constructor is something like:

Then “this.myControl = new MyControl(this);” in MyParentControl.Designer.cs will be deleted as shown below.

One workaround is simply to create an empty constructor for the usercontrol, and perform the actual initialization in MyParentControl’s constructor like this:

While not exactly elegant, it does gets the job done until the component designer bug is fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *