How to limit the size of a form based on a contained control

R

Roy Chastain

Currently this code has to work in .Net 1.1

I have a form. I has a control (based on Panel) that occupies a large portion of the form. The control is anchored on all 4
sides. It is not docked on any side. The panel control resizes and repositions its contents based on its size.

There is a minimum useful size for the panel control. I would like to limit the minimum size of the form based on the panel
reaching its minimum size.

My first thought is to set the minimum size of the form to the size of the panel plus its distance from the edges of the form.
This would work for the simple case, but if the panel is placed inside another container control that is on the form then things
get more messy.

Is there a better way?

Thanks
 
L

Linda Liu [MSFT]

Hi Roy,

Based on my understanding, you have a panel on a form and anchor the panel
on all 4 sides. The panel has a minimun size and you want to limit the
minimum size of the form as well. You have managed to do this by setting
the MinimumSize property of the form. However, if the panel is placed
inside another container, i.e. a panel that is on the form, things get
messy. If I'm off base, please feel free to tell me.

Suppose we have a Panel control named panel1 on the form and anchor panel1
to all 4 sides and then configure the MinimumSize property of panel1. We
could set the MinimumSize property of the form in the form's Load event
handler according to the minimum size of panel1. The following is a sample
code.

private void Form1_Load(object sender, EventArgs e)
{
Size size = new Size();
size.Width = this.panel1.MinimumSize.Width + this.Width -
this.panel1.Width ;
size.Height = this.panel1.MinimumSize.Height + this.Height -
this.panel1.Height ;
this.MinimumSize = size;
}

If panel1 is placed in another panel control, i.e. panel2, we could anchor
panel2 to all 4 sides and needn't do anything else. When the program runs,
you will find the panel1 and panel2 resize when the form is resized and the
minimum size of the form works correctly.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Roy Chastain

You have a correct understanding of the problem, but your solution leaves a bit to be desired.

In your solution the form has to have special code to handle the panel. It would be better if the panel were a general use module
that could affect the behavior of the form with the form knowing that the panel needed special handling.

Also, at least in 1.1, Panel does not have a MinimumSize property (which in my mind is the real problem). If container controls
just a Panel had a MimimumSize property that was honored then the form could never force the container control to become to small.
Sizing control should not just be for Forms .
 
L

Linda Liu [MSFT]

Hi Roy,

Thank you for your prompt response.
It would be better if the panel were a general use module that could
affect the behavior of the form with the form knowing that the panel needed
special handling.

We could derive a custom panel from Panel class and add a public property
for minimum size or an event in the custom panel to let the form know that
the cusom panel needs special handing. However, there will be a new
question that when the event should be raised. Unfortunately, it seems it
is difficult to find the time to fire the event.

No matter what method we use to inform the form to know that the custom
panel needs special handling, the actual work to limit the minimum size of
the form is done in the form. And I think the simplest way to set the limit
is to set the MinimumSize property of the form.

Yes, you're right that in .NET 1.1, Panel doesn't have a MinimumSize
property (in .NET 2.0, Panel has this property). But never mind, as I
mention above, we could add a property for the minimum size in the custom
panel class and then set the MinimumSize property of the form according to
the custom panel's minimum size property in the form's Load event handler.
I have performed a test on this and it works.

When the panel is placed in another container control, I think the key
point is to anchor the container control to all 4 sides as well. Then when
the form is resized, the container control is resized, which will then
cause the panel within the container control to be resized as well. In this
case, the code of setting the MinimumSize property of the form still works.
If container controls just a Panel had a MimimumSize property that was
honored then the form could never force the container control to become to
small. Sizing control
should not just be for Forms .

I am sorry that I couldn't undertand what you mean in the above sentences.
Would you please explain more to me?

Hope this helps and I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
R

Roy Chastain

If container controls just a Panel had a MimimumSize property that was
honored then the form could never force the container control to become to
small. Sizing control

I am sorry that I couldn't undertand what you mean in the above sentences.
Would you please explain more to me?

What I am saying is that every control on a form should have a min and max size specification. Anytime a resize is started at the
form level, the form should check for any resizing controls to see if their min/max is being violated. If it the min/max is
violated then the resize is limited to a size that would not violate the min/max of any resizing control.

I have not really arrived at a satisfactory solution to this issue as yet. Currently I have code in the control's OnParentChanged
that sets the parents min size according to the correct calculations. That works as long as the control is directly on the form.
It will not work (not tested but assuming so) when the control is inside another control because then the ParentChange would be
when it is attached to its container. Expecting all containers to propagate the event when they are attached to their owners is
not reasonable.

The other choice would be to put this in formload like you suggested and implement an IConstrainedResize interface that provides a
min/max size of the control that would allow the form to set its size. Problem is once again if the control in question is
contained in another control, I would have to create my on custom container controls that inherit from the supplied containers and
implement my IConstrainedResize interface and propagate it through. Too much overriding and inheritance.

Looking for yet a better solution.

Thanks
 
L

Linda Liu [MSFT]

Hi Roy,

Thank you for your update.

I understand what you want and I think it's reasonable. Unfortunately, our
product doesn't support such a function at least now. But I think we could
implement this absolutely by some lines of code and it won't require too
much work.

The basic thinking is to set the MinimumSize property of the form according
to the 'MinimumSize' properties of the controls in it. For those controls
that haven't such a MinimumSize property, we could inherit a new control
from the previous sandard control and add properties for the 'MinimumSize'
in the derived control.

Then in the form's Load event handler, we calculate the minimum size of the
form that each container control(e.g panel, usercontrol and so on) on the
form requires, including the controls contained in another controls, and
then choose the biggest value as the minimum size of the form. You needn't
do anything except defining properties for minimum size in the custom
controls or user controls. To make the controls resize properly with the
form, anchor them to the 4 sides, including those controls contained in
another controls.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
R

Roy Chastain

I agree with the basic concept that you are presenting, but again this is a real pain to deal with. It means that I have to
override every control that needs to implement a min/max size parameter.

Let us assume for a minute that I do the overriding by implementing a new interface called IConstrainedControl and it provides 2
new properties, SizeMin and SizeMax. So for so good. The OnLoad event code and query each child control for min and max sizes
and do the proper manipulation of the forms min size.

What happens if one of these controls is a container control such as
public class ConstrainedPanel: Panel, IConstrainedControl
{ ... }

It appears that only UserControls and Forms have an OnLoad event, so the ConstrainedPanel control would not have an OnLoad event.

The question is
At the time that the form's load event occurs, will all the controls contained in the ConstrainedPanel be instantiated and be able
to propagate any min/max sizes up to the ConstrainedPanel container?
 
L

Linda Liu [MSFT]

Hi Roy,
It means that I have to override every control that needs
to implement a min/max size parameter.

Yes, you're right. But I think you only need override some container
controls, such as Panel, GroupBox and so on. Those controls like
TextBox,ComboBox, ListView and etc, needn't implement a min/max size
parameter.

Assume that you have a form with a derived container control on it and the
container control has some child controls,e.g. textbox, combobox within it.

To simplify the process, I suggest that you determine the min/max size of
the derived container control according to the child controls in it at
design-time, set the properties and then calculate the min/max size of the
form according to the min/max size properties of the container control in
the form's Load event handler(you could ignore the child controls within
the container control).

If a derived container control A is placed within another container control
B, you calculate the values of min/max size of the form that are required
by control A and B respectively and then choose the bigger value as the
min/max size of the form.
It appears that only UserControls and Forms have an OnLoad
event, so the ConstrainedPanel control would not have an OnLoad event.

I agree with you. I think we could avoid this problem by doing the
calculation in the form's Load event handler, as I mention above.
At the time that the form's load event occurs, will all the controls contained
in the ConstrainedPanel be instantiated and be able to propagate any
min/max sizes up to the ConstrainedPanel container?

At the time that the form's Load event occurs, all the controls on the form
have been instantiated and we could get any min/max size of the controls if
they have. As I say above, I suggest that you set the min/max size of the
controls that are placed on the form directly at design-time according to
the child controls in it and then calculate the min/max size of the form
according to these controls placed directly on the form.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
R

Roy Chastain

Thanks Linda, I think I can take it from here.

Hi Roy,


Yes, you're right. But I think you only need override some container
controls, such as Panel, GroupBox and so on. Those controls like
TextBox,ComboBox, ListView and etc, needn't implement a min/max size
parameter.

Assume that you have a form with a derived container control on it and the
container control has some child controls,e.g. textbox, combobox within it.

To simplify the process, I suggest that you determine the min/max size of
the derived container control according to the child controls in it at
design-time, set the properties and then calculate the min/max size of the
form according to the min/max size properties of the container control in
the form's Load event handler(you could ignore the child controls within
the container control).

If a derived container control A is placed within another container control
B, you calculate the values of min/max size of the form that are required
by control A and B respectively and then choose the bigger value as the
min/max size of the form.


I agree with you. I think we could avoid this problem by doing the
calculation in the form's Load event handler, as I mention above.


At the time that the form's Load event occurs, all the controls on the form
have been instantiated and we could get any min/max size of the controls if
they have. As I say above, I suggest that you set the min/max size of the
controls that are placed on the form directly at design-time according to
the child controls in it and then calculate the min/max size of the form
according to these controls placed directly on the form.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top