ehh February 2016

Defining a Winform as abstract do not allow editing the UI of the derived Winforms

I have a base winform and 2 derived winforms. The base winform contains labels, textboxes and a save button. Each derived class contains additional labels and textboxes. The SaveButton_Click event is calling to a Save method. I defined the Save method as abstract in the base class therefore I am also defining the base winform as abstract.

Here is my code:

public abstract partial class BaseRowInfo : Form
{
    public BaseRowInfo()
    {
        InitializeComponent();
    }

    private void SaveButton_Click(object sender, EventArgs e)
    {
        Save();
    }

    protected abstract void Save();
}

public partial class EditableRowInfoFrm : BaseRowInfo
{
    public EditableRowInfoFrm():base()
    {
        InitializeComponent();            
    }

    protected override void Save()
    {
        // TODO
    }
}

public partial class ReadOnlyRowInfoFrm : BaseRowInfo
{
    public ReadOnlyRowInfoFrm ():base()
    {
        InitializeComponent();            
    }

    protected override void Save()
    {
        // TODO
    }
}

Once I am defining the base class as abstract, I do not have anymore the ability to edit the UI of the derived forms. Does the fact that I am defining the Base Class as abstract is wrong? What is the solution in case it is acceptable to define it as abstract?

Answers


Beniamin E. February 2016

If you want to be able to edit UI controls of the abstract class in the derived class mark those controls with 'protected' access modifier instead of 'private'.


Patrick Hofman February 2016

No, there is nothing wrong with your approach. The Visual Studio IDE just doesn't like you for using abstract classes in the editor.

I can understand from Microsofts point-of-view this is very hard to implement. You risk changing something from the base class. I am not sure how they would implement a designer that would work in a decent way.

There are two options:

  • Write the UI code by hand for every deriving class;
  • Add a control, possibly implementing a common interface for communication, that you can add into your derived form. That control can be created using the designer.

Post Status

Asked in February 2016
Viewed 2,546 times
Voted 12
Answered 2 times

Search




Leave an answer