How come my forms wont resize...

  • Thread starter Thread starter Ty Salistean
  • Start date Start date
T

Ty Salistean

I have a form that I inherited from System.Windows.Forms. I use that form
everywhere in my system. I went back to my base class form and changed the
size, expecting all of my children forms to inherit the base class forms
size.

This is not what happened. All of my children forms thought the size
property was overridden. They stayed the old size.

How can I get my children forms to respect the parents size property? I am
assuming this is not specific to the size property. How can I get my
children forms to repsect any parent form property without visiting each
form?
 
Hi,

No really, you see by default the form constructor call Initialize() that is
where all the controls and form properties are assigned ( this is the
designer's gneraet code) s. This method is not virtual, so what happens is
that your derived form call first its parent ( your template form) the
latter in its constructor initialize it self, then the derived constructor
runs and as part of this a call to the derived Initialized method is
performed, hence overwritting the parent's value.

Cheers,
 
This probably involves monkeying around with the DefaultValue()
attribute on the Size property of your inherited form, or implementing
a ShouldSerializeSize() method. Here is what is going on now:

Each property of every Control can define a DefaultValue attribute for
that property. This attribute tells Visual Studio Designer that if the
value of that property is equal to the given default value, don't
bother serializing the property in the "Windows Designer Generated
Code" section of your class. For example, the default value for the
MultiSelect property of the ListView control is true, so if you leave
it at true, Visual Studio Designer won't insert any code to initialize
that property. Only if you change it to false will you see:

this.lvwBusinessObjects.MultiSelect = false;

Since your redefined Form doesn't define a new default value for the
form size, Visual Studio Designer will _always_ serialize the size of
the form or of any child forms, because the form's size is different
from the default established on the base Form class (if there even is
one). So, in the code marked "Windows Designer Created Code" there will
always be a line like this:

this.Size = new Size(400, 500);

because Visual Studio Designer sees that the form size is not equal to
the default of 300, 300, and so it thinks that it has to insert code to
initialize the size. (Or maybe there is no default defined for Form...
I don't know.)

Anyway, you have to tell Visual Studio to not bother serializing the
form size if it is equal to that of the parent form. Unfortunately,
there's no way to do this other than hard-coding the form size into the
parent Form's class. I would try the following to see if it works.
Define a new method called ShouldSerializeSize() in your parent Form:

private bool ShouldSerializeSize()
{
return this.Size.Width == 1000 && this.Size.Height == 500;
}

for example. This should cause Visual Studio Designer to check the
child form's size to see if it is 1000 x 500. If it is, the Designer
won't insert any code to set the size of the child form.

The only drawback is that whenever you change the size of your parent
form, you have to remember to fix the ShouldSerializeSize() method.
(And no, you can't just compare this.Size.Width with base.Size.Width or
something like that. If you think about it you'll realize why that
wouldn't work.)

I'm still learning about exactly how the Designer works with defaults
and the class hierarchy, so don't be surprised if this doesn't work at
the outset. Let us know what happens and we can try other solutions if
this doesn't do it.
 
How can I get my children forms to respect the parents size property?

You realize, of course, that getting children to respect their parents'
_anything_ is a lifelong challenge? ;-)
 
private bool ShouldSerializeSize()
{
return this.Size.Width == 1000 && this.Size.Height == 500;
}

Oh crap. That's backward. I meant:

private bool ShouldSerializeSize()
{
return this.Size.Width != 1000 || this.Size.Height != 500;
}
 
Here is what I found out.

If you take the scenario that I posed in my first post AND you do not have
any controls on your child form, then the form behaves as expected. I set
the width property back 100 pixles on the parent and my child responds
appropriately.

The second that I put controls on my child form, the child form adds an
entry to the Initialize Components
this.ClientSize = new System.Drawing.Size(1012, 273);

Even if I right click on the property on the child form and click "Reset to
Default", this line of code is not removed. It simply gets set to the
parents size.

I guess (after thinking about this) this is not a bad thing. If I make the
parent form really tiny, then I run the risk of hiding controls on the child
forms where that propery has not been overridden.

It is a bad thing cuz if you want to develop an app at 800x600 knowing that
you will move to 1024x768, you have to go to all of your child forms and
revisit this setting. That may not be a bad thing cuz your controls would
need to get moved or resized to accomodate the new design area.

I think if you weigh the factors in here, I think I would actually agree
that this is a good behavior. It looks like this will be one of the only
properties affected, given what I am seeing in the Initialize Components.

I tested that with the Minimizebox property on the parent form. The child
form did get the change appropriately.

Thanks for the post - but I think I am going to leave this one alone. It
could keep me from doing stupid things...
 
Back
Top