2
votes

In my project I have a custom panel:

[Serializable]
public partial class ButtonPanel : UserControl
{
    private List<CompactButton> _compactButtons;

    public ButtonPanel()
    {
        InitializeComponent();

        _compactButtons = new List<CompactButton>();

        AddButtons();
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
    public List<CompactButton> CompactButtons
    {
        get { return _compactButtons; }
        set { _compactButtons = value; }
    }

    private void AddButtons()
    { 
        CompactButton c = new CompactButton();
        c.Enabled = baseButton1.Enabled;

        CompactButton c2 = new CompactButton();
        c2.Enabled = baseButton2.Enabled;

        _compactButtons.Add(c);
        _compactButtons.Add(c2);
    }
}

On a panel there are two buttons (baseButton1 and baseButton2). The state of these buttons must be saved in the designer. But because the Button class isn't serializable, I have created a custom class to store the most important properties, CompactButton:

[Serializable]
public class CompactButton
{
    #region Member variables
    private bool _visible;
    private bool _enabled;
    private string _tooltip;
    private string _name;
    #endregion

    #region Constructor
    public CompactButton()
    { }
    #endregion

    #region Properties
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public bool Visible
    {
        get { return _visible; }
        set { _visible = value; }
    }
    public bool Enabled
    {
        get { return _enabled; }
        set { _enabled = value; }
    }
    public string ToolTipText
    {
        get { return _tooltip; }
        set { _tooltip = value; }
    }
    #endregion
}

As you can see in the ButtonPanel class, I have a property CompactButtons which returns a list with CompactButtons.

When I add my ButtonPanel in the designer and then build my application I get this error:

Error 1   Could not find a type for a name.  The type name was

'System.Collections.Generic.List`1[[ButtonPanelX.CompactButton, ButtonPanelX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Line 131, position 5. D:\Projecten\ButtonPanelX\ButtonPanelX\Form1.resx 131 5

What do I have to do to fix this?

1
+1: interesting problem. My first thought was, CompactButtons isn't serializable. However, you have that covered. My secondary thought is to maybe derive CompactButtons from ISerializable. - IAbstract
Thank you for commenting on me. I haven't try deriving from ISerializable. If I do this, do I have to implement all the methods? Or just use the default ones. - Martijn
You must implement all the methods. - IAbstract
I'm also now wondering if you need to use another attribute. There is one that is suppose to make something design-time visible; I just don't recall the name of the attribute. You could also have CompactButtons derive from Control. - IAbstract
I can't derive from Control because it isn't serializable. Do you have other suggestions I can try? I have also tried to implement ISerializable but I still get the same error - Martijn

1 Answers

0
votes

I made a new project, pasted your example and it just works for me (after removing the references to stuff not included in the sample).

From your error message I suppose you changed something in the code and recompiled, now the RESX serialized format is no longer valid. Note that once you got serialized data you are very restricted in what you can change in the class you serialized.

I'd suggest removing the controls from the designer file and resx file manually (open the resx with the xml editor) and place the control again.

One additional hint I have: You don't need to use [Serializable] on CompactButton at all. If you remove it VisualStudio will serialize by code into the designer.cs file instead of the resx file. I almost always prefer this because then I can manually edit the generated code when something changes.