Making multiple forms same size and position

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,


I have a Windows Forms application written in .NET 2.0 made up of multiple
forms. These forms use visual inheritance to share a common layout while
presenting some specific content.
The application switches among the different forms by showing/hiding them
making only one form visible at a time.

The problem arises when the user maximizes, resizes or repositions the
active form. What happens is that whenever another form is made visible this
won't have the same window status or position of the previous one which is
visually very disturbing.

I still haven't found any good solution to this problem. Do you have any
suggestions?
 
Hi Enrico,

Thank you for posting.

I think in order to make a form has the same window status or position of
another form, we should save this information such as size or location to a
"public" variable to which all the forms in your application could access.
Since the forms in your application are all derived from a base form class,
we could add some static variable to the base form class. Then we could add
a VisibleChanged, SizeChanged and LocationChanged event handlers in the
base form class to get and set the static variable.

The following is a sample.
public partial class BaseForm : Form
{
// static variable
private static Point formlocation = new Point(0,0);
private static Size formsize = new Size(300,300);
private static FormWindowState formwindowstate =
FormWindowState.Normal;

public BaseForm()
{
InitializeComponent();
this.VisibleChanged += new EventHandler(Form2_VisibleChanged);
this.SizeChanged += new EventHandler(Form2_SizeChanged);
this.LocationChanged += new
EventHandler(Form2_LocationChanged);
}

void Form2_VisibleChanged(object sender, EventArgs e)
{
this.WindowState = formwindowstate;
this.Size = formsize;
this.Location = formlocation;
}
void Form2_LocationChanged(object sender, EventArgs e)
{
formlocation = this.Location;
}
void Form2_SizeChanged(object sender, EventArgs e)
{
formwindowstate = this.WindowState;
formsize = this.Size;
}

}

Hope this is helpful to you.
If you have other conerns or need anything else, please don't hesitate to
let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Hi Linda,

Your solution solved the problem, thanks a lot!

P.S I noticed that under some circumstances the current form position or
size are not correctly updated in the common state stored in the base class.
I am trying to isolate the problem but it looks some like some
synchronization issue. The event handlers for 'PositionChanged' and
'SizeChanged' are somewhat delayed which sometimes causes forms to receive
outdated values when becoming visible.
 
Hi Enrico,

Thank you for your quick response.

If these forms are displayed as MDI child forms in your program, the
position or windows status of these MDI child forms may be incorrect. This
problem is related to the MDI parent form which may change the behavior of
its MDI child forms.

Hope this helps.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Back
Top