How to fix WinForms designer RESX problem with custom control

Steve Marshall 0 Reputation points
2024-02-15T01:05:11.8266667+00:00

Using: Visual Studio 2022, latest updated version. Developing a WinForms custom control for .NET Framework 4.8. Platform is set to Any CPU. I am experimenting with building a custom control. The eventual aim is that it will display a graph in a rectangular area inside it, and it will have a number of text fields in and around the graph. One of its properties is therefore a Collection of these text fields. I started by making this a collection of Label objects, but that didn't work, because VS complained that Label is "not marked as serializable". I thought I could get around it by making my own text field inheriting from Label, but the not-serializable error still occurred. So, I made my own object with a subset of the properties a Label has. This object does not inherit from any other control or object, and is (of course) marked as Serializable. If I make a small project to test this, the form designer works beautifully - the property which is my collection of labels shows as (Collection) in the Properties window for the control, and I can go into it and add and edit instances of my label object. And the labels appear where they should. I can compile and run the test app, and things work as expected. BUT - if I close the form in the design mode and try to reopen it, the designer throws the error "Object of type QDLGraph.GraphLabel[] cannot be converted to type QDLGraph.GraphLabel[]". It seems unable to read and interpret the data which it has written to the form's RESX file, which contains the info about the properties of the collection of labels in my control. I am struggling to see how this is anything but a bug in Visual Studio - it has total control over what is written to the RESX file. So if there is something invalid in there, it can only be Visual Studio which put it there. I have done quite a bit of searching about this but have not come across any definitive solution. I saw a post from a few years back that suggested it had been fixed in a VS update, but I have the latest VS 2022 and it would appear not. If I am missing something here, or if someone has found a way around this, I would be very happy to be enlightened. At the moment this is a total show-stopper, and I cannot make any progress.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Steve Marshall 0 Reputation points
    2024-02-15T01:41:07.28+00:00

    I have found the problem here. Somewhat to my surprise, it seems it is necessary to tell VS that the property needs to be serialized. So in the code for the Graph control, the property for the label collection needs this attribute: [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] Once I added that, all is well. Hope this might help someone else.

    0 comments No comments

  2. Jiale Xue - MSFT 46,466 Reputation points Microsoft Vendor
    2024-02-15T07:11:04.13+00:00

    Hi @Steve Marshall , Welcome to Microsoft Q&A,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue: Solution in customer verbatim: Somewhat to my surprise, it seems it is necessary to tell VS that the property needs to be serialized. So in the code for the Graph control, the property for the label collection needs this attribute:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    

    Once I added that, all is well.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.