Creating custom .Net controls

I am currently creating a .Net custom form using C # and I am filling it with custom controls. Each control has an accessor that receives and sets an object that contains the data that the control will populate.

At runtime everything works fine, but at design time I get errors in the form designer. Errors always go along the lines: "Unable to convert object of type [ObjectA] to object of type [ObjectA]"

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

Once I have done this, the form will be displayed in the constructor until it restores InitializeComponet and inserts the rows / data into resx and InitializeComponent.

What am I missing in my management and the class that will end this cycle? I've tried using Liscence's use mode and Designer runtime with mixed results, and I'd rather be able to solve this in my design.

Thanks for any help you can provide.

Update: I added an attribute ...

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

      

Into the property and I got an error in the constructor "ObjectA is null, this is not allowed!" So I changed the line to ...

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

      

and the problem was gone. Since I don't need to set any of these properties at design time, a hidden attribute is probably more appropriate.

Thank.

+1


source to share


2 answers


It looks like this might be the way the object is serialized.

The designer is doing binary serialization of the object to a resx file, perhaps you need some code to be generated in your .Designer.cs file.



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

+2


source


Here is some sample design-oriented code in a custom control:

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

      



It may not work for you, but if you end up any problematic code in the "if (**! ** this.DesignMode) {}" block, you should be fine (unless you need this code for the control to rendering correctly in design mode).

0


source







All Articles