0

I have a very basic "base" usercontrol with some added properties. When I inherit from the base usercontrol and add some buttons to it in the designer everything seems to be fine. But when I build my project all of my buttons disappear and the layout restores back to the original base usercontrol. What am I doing wrong here?

public partial class BaseUserControl : UserControl
{
    public BaseUserControl()
    {
        InitializeComponents();
    }

    public bool CanClose
    {
        get;
        set;
    }
}

public partial class InheritedUserControl : BaseUserControl
{
    public InheritedUserControl()
    {
        InitializeComponents();
    }
}
Cris McLaughlin
  • 1,201
  • 2
  • 14
  • 23
  • Let me start with saying that control inheritance is an absolute nightmare. Is it possible that your `InitializeComponent()` call on the base is being called after the child (in some designer automagic fashion), which is loading the base components? – Brandon Sep 10 '14 at 15:09
  • @Brandon *Let me start with saying that control inheritance is an absolute nightmare* I disagree. I'm using control inheritance extensively and that works pretty well. Could you explain a bit about why it is nightmare? – Sriram Sakthivel Sep 10 '14 at 15:12
  • I assume the base Initialize gets called first then the inherited is called next? – Cris McLaughlin Sep 10 '14 at 15:18
  • This sort of thing comes up a lot when dealing with UserControls. See this for a start...it's not a dupe, but I think the underlying issues are always the same: http://stackoverflow.com/questions/207504/datagridview-locked-on-a-inherited-usercontrol – DonBoitnott Sep 10 '14 at 15:41
  • @SriramSakthivel Because of stuff like this question. You can code a constructor for your control or some properties and they'll be treated differently at design vs. run time. 75% the time the properties don't save or you get some error where visual studio crashes. Designer doesn't load properly etc.. Not sure that this is the place for this discussion though. – Brandon Sep 10 '14 at 19:25
  • @Brandon If designer doesn't load, you need a clean and rebuild that should do the trick. If not you shouldn't run code which isn't allowed in design time.You could prevent it by `if(!DesignMode){DoStuff();}`. For properties not saving you have to use proper `DesignerSerializationVisiblityAttribute`. For default values you need to use `DefaultValueAttribute` even though you set properties in constructor. So I guess you might be missing something or I may not see the problem which have seen. Designers are tricky but if done right they can be very useful. Nevertheless I use designer very little. – Sriram Sakthivel Sep 10 '14 at 19:36

0 Answers0