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:
1 2 3 4 5 6 7 8 9 10 11 12 | partial class MyControl : UserControl { private MyParentControl myParentControl; public MyControl(UserControl myParentControl) { InitializeComponent(); this.myParentControl = (MyParentControl)myParentControl; } // ... } |
Then "this.myControl = new MyControl(this);" in MyParentControl.Designer.cs will be deleted as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // MyParentControl.cs partial class MyParentControl : UserControl { public MyParentControl () { InitializeComponent(); } } // MyParentControl.Designer.cs partial class MyParentControl : UserControl { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { ... } #region Component Designer generated code private void InitializeComponent() { // -- Component Designer deletes this line below -- // this.myControl = new MyControl(this); ... private MyControl myControl; } } |
One workaround is simply to create an empty constructor for the usercontrol, and perform the actual initialization in MyParentControl's constructor like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | partial class MyControl : UserControl { private MyParentControl myParentControl; public MyControl() { InitializeComponent(); } // -- Additional initialization method required -- // public void Initialize(UserControl myParentControl) { this.myParentControl = (MyParentControl)myParentControl; } // .... } // MyParentControl.cs partial class MyParentControl : UserControl { public MyParentControl () { InitializeComponent(); // -- Initialize from here instead -- // this.myControl.Initialize(this); } } // MyParentControl.Designer.cs partial class MyParentControl { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { ... } #region Component Designer generated code private void InitializeComponent() { // -- Component Designer doesnt delete this anymore! -- // this.myControl = new MyControl(); ... private MyControl myControl; } } |
While not exactly elegant, it does gets the job done until the component designer bug is fixed.
Print This PostShowing User Controls On Toolbar C#
User controls are very useful for creating reusable code. Adding them as separate projects to your solution, and referencing them would have these user controls appearing on your toolbar by default.
However, user controls when placed in the same project in visual studios some times don't appear on your toolbar, and you have to keep deleting the "obj" files to refresh the cache. Recently had this experience when loading up some code on another workstation. The fix suggested from MSDN, which took a while to find, worked. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=106059&wa=wsignin1.0
Basically, the solution is to go Tool > Options > Windows Form Designer and ensure that the AutoToolboxPopulate option is set to True.

Enable AutoToolboxPopulate Option
