I want to have an abstract UserControl, BaseControl, that implements an interface IBaseControl.
However, setting the class to abstract breaks VisualStudio designer (This is a known issue with Visual Studio (e.g., see this StackOverflow posting for more info), and as far as I know, there's no change expected in the near future
So, to work around this, I make BaseControl not abstract, and its implementation of IBaseControl methods virtual. However, since these methods make no sense for a BaseControl (e.g., not all components have been added yet), I make them throw:
public class BaseControl : UserControl, IBaseControl
{
/// <summary>
/// This IBaseControl method is not abstract because
/// that breaks the Designer
/// </summary>
public virtual void LoadSettings()
{
throw new NotImplementedException("Implement in derived class.");
}
private void BaseControl_Load(object sender, EventArgs e)
{
// intention: derived methods automagically load their settings
this.LoadSettings();
}
}
In the derived control, I have the corresponding override:
public partial class DerivedControl : BaseControl
{
public override void LoadSettings()
{
// load settings
}
}
Despite this, when I try to open the control in the designer, I get an error indicating that the BaseControl.LoadSettings has thrown an exception.
Now, remember LoadSettings is called in the base class, so when the Designer loads the DerivedControl, it in turn calls the load method for the BaseControl, which throws.
Have you encountered a similar problem? How have you dealt with this? I'd like to have an elegant solution, if possible.