views:

133

answers:

1

I have subclassed Form to include some extra functionality, which boils down to a List<Image> which displays in a set of predefined spots on the form. I have the following:

public class ButtonForm : Form 
{
    public class TitleButton
    {
        public TitleButton() { /* does stuff here */ }
        // there's other stuff too, just thought I should point out there's
        // a default constructor.
    }

    private List<TitleButton> _buttons = new List<TitleButton>();
    public List<TitleButton> TitleButtons
    {
        get { return _buttons; }
        set { _buttons = value; }
    }
    // Other stuff here
}

Then my actual form that I want to use is a subclass of ButtonForm instead of Form. This all works great, Designer even picks up the new property and shows it up on the property list. I thought this would be great! It showed the collection, I could add the buttons into there and away I would go. So I opened the collection editor, added in all the objects, and lo and behold, there sitting in the designer was a picture perfect view of what I wanted.

This is where it starts to get ugly. For some reason or another, Designer refuses to actually generate code to create the objects and attach them to the collection, so while it looks great in Design mode, as soon as I compile and run it, it all disappears again and I'm back to square one. I'm at a total loss as to why this would happen; if the Designer can generate it well enough to get a picture perfect view of my form with the extra behaviour, why can't/won't it generate the code into the actual code file?

+3  A: 

First of all you need to inherit your TitleButton class from Component so that the designer knows it is a component that can be created via designer generated code. Then you need to instruct the designer code generator to work on the contents of the collection and not the collection instance itself. So try the following...

public class TitleButton : Component
{
   // ... 
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<TitleButton> TitleButtons
{
   // ...
}
Phil Wright
Well, that was amazingly simple... Thankyou muchly. You seem to be my personal saviour as of late :D
Matthew Scharley
Something is still bugging me about this though... If it needs to be told that it's a Component that it can work with, why will it work with it anyway and then just not write any code?
Matthew Scharley