views:

178

answers:

2

I am currently building a .Net userform using C# and I am populating it with custom user controls. The controls each have an accessor that gets and sets the object that contains the data that the control will be populated with.

At runtime, everything works great, but at design time I will get errors in the form designer. The errors are always along the lines of "Cannot convert an object of type [ObjectA] to an object of type [ObjectA]"

At this point, I can go into the resx file and delete the row that references the object of type ObjectA and then go into the designer.cs file and delete the line in InitializeComponent that sets the accessor of the control to the data from the resx file.

Once I've done that, the form will display in the designer until it rebuilds the InitializeComponet and reinserts the lines / data into the resx and InitializeComponent.

What am I missing in my control and class design that will end this cycle? I've tried using the Liscence Usage mode and the Designer runtime mode with mixed results and I would prefer it if I could solve this in my design.

Thanks for any help that you can provide.

Update: I added the attribute...

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

To the property and I got an error in the designer "ObjectA is null, this is not allowed!", so I changed the line to ...

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

and the issue went away. Since I don't need to set any of these properties at design time, the hidden attribute is probably more appropriate.

Thanks.

A: 

Here is an example of design-time-aware code in a user control:

protected override void OnPaintBackground(PaintEventArgs e)
{
    if (this.DesignMode)
    {
        base.OnPaintBackground(e);
    }
}

This may not work for you, but if you wrap any problematic code in an "if (!this.DesignMode) {}" block, you should be fine (unless you needed that code for the control to render correctly in design mode).

MusiGenesis
+2  A: 

It sounds like it could be the way that the object is being serialized.

The designer is doing a binary serialization of the object into the resx file, what you probably need is code to be produced in you .Designer.cs file.

Try putting the following attribute line on the property: [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

benPearce