views:

337

answers:

3

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.

A: 
public partial class derivedForm : baseForm
{
    public derivedForm()
    {
        InitializeComponent();

        this.Width = base.Width;
        this.Height = base.Height;
    }
}
sgrassie
Yeah, there's always that way, but I want the derived forms to inherit their size from the base form automatically if possible.
dandan78
Is this size going to be the same initial size only or is it going to be a non-resizable form where the size is static?
Steve Danner
Just the initial size. Right now it's the default 300x300.
dandan78
A: 

Why not make the BaseForm set the size of itself?

public partial class BaseForm : Form
{
    public BaseForm()
    {
        InitializeComponent();

        // you could hardcode these or retrieve these values from a
        // config file or something
        this.Width = 640;
        this.Height = 468;
    }
}

Wouldn't this do what you want?

kitchen
Tried that, no go.
dandan78
+1  A: 

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:
        ...
    }
}
stakx
Right you are. Form Designer inserted the line `this.ClientSize = new System.Drawing.Size(292, 273);`. When commented out the form is the right size. Is there any way to prevent Form Designer from doing that?
dandan78
Yes there is, I just expanded my answer accordingly. See the "workaround solution" section in the middle.
stakx
As you pointed out, the workaround seems a bit non-standard, but it looks like it should work. Nevertheless, I'll try it and if nothing better comes up, you get the checkmark. :)
dandan78