Print This Post
13
Jul/09
0

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.

Filed under: IDE tweaks
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

You must be logged in to post a comment.

No trackbacks yet.

Copy Protected by Chetan's WP-CopyProtect.