Winform inheritance and default form size

dandan78 picture dandan78 · Feb 12, 2010 · Viewed 8.1k times · Source

An application I'm working will have a number of forms with a lot of shared functionality. For instance, each form will have a DataGridView, many of the same buttons, much of the same UI code and so on.

I'd like to implement this by creating a base version of this common form, subclass it for all these very-similar-but-not-quite-the-same child forms, and tack on whatever additional controls and features I need for each of them.

I've already figured out that it helps to make the base form's controls protected because this allows things like anchoring to work propertly. However, I have yet to find a way to automatically make the derived forms the same size as the base form.

Experience tells me there should be a simple way to do this. While it's not much of a problem to just type in the required size by hand for every derived form right after creating it, I'd prefer to make everything as clean, simple, and automatic as possible.

Answer

I find it interesting that your derived forms do not automatically inherit the size from their base form, because this should work without you having to do anything about it.

Suspected cause of your problem:

I suspect your problem results from the fact that you're using Visual Studio's Forms Designer to edit the forms. Whenever you've edited a form, Windows Forms Designer generates the required code in the InitializeComponent method of your forms. Among all the generated code are assignments that set a form's size, even if it is identical to the base form's size. Therefore you might have to manually comment out those assignments if you want your derived form to have the same size as the base form, even when you change the base form's size after creating the derived forms. (However, I don't know if that might lead to further problems with the controls' positioning & layouting.)

// Code to be commented out in your derived form's InitializeComponent method:
this.AutoScaleDimensions = new System.Drawing.SizeF(...);
this.ClientSize = new System.Drawing.Size(...);

Once these lines are commented out, the size as set in your base form's InitializeComponent will be used for the derived form.

A workaround solution:

You can do the following so that you don't have to manually comment-out designer-generated code every time you've edited a form:

Create an form derived from your base form; let's call it FrozenBaseForm. You will derive all other forms from this class instead of directly from the base form. Now, in this "intermediate" class, you define a new property ClientSize:

public class FrozenBaseForm : BaseForm
{
    new public SizeF ClientSize
    {
        get { return base.ClientSize; }
        set { }
    }
}

This will cause all assignments to ClientSize to have no effect at all and therefore preserve the size from the base form. This feels like a hack to tell the truth, but it seems to work. You might have to hide the Size property in the same way btw.

As said, derive your forms from FrozenBaseForm instead of from BaseForm directly:

public class DerivedForm1 : FrozenBaseForm { ... }
public class DerivedForm2 : FrozenBaseForm { ... }
...

Another option (last resort if all else fails):

As a last resort, you could simply forget about the Forms Designer and just define the derived forms manually in the code editor (though I personally would not want to do this):

public class DerivedForm : BaseForm
{
    public DerivedForm()
    {
        // make all necessary changes to the base form:
        ...
    }
}