Problem restoring Form size on launch.

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

Guest

Hi,

I'm saving the bounds of my Form in the Registry in my Form's closing event
and restoring the bounds when my app is launched. Everything works well
except the persisted Height increases by 20 each run of my app. No other
dimensions change. My Form has a main menu and a status bar and both are
part of the Form at design time (I had suspected the Height increase to come
from the Height of one of these controls). Any ideas?

Thanks,
Jack
 
How are you setting the size? Are you assigning to the Size property or
Bounds property or other? Post some code.
 
I'm using Bounds (although I did try Left, Top, and Size also).

Here's some code:

My Form constructor:

public FormMDIContainer()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
Rectangle bounds = Options.Instance.ApplicationOptions.Bounds;
this.Bounds = bounds;
}

And my Form Closing handler

private void FormMDIContainer_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Rectangle bounds = this.Bounds;
Options.Instance.ApplicationOptions.Bounds = bounds;
Options.Instance.ApplicationOptions.PersistOptions();
}

The Options class is a singleton that persists and loads my app options to
and from the Registry. The lines that load (called in the ApplicationOptions
constructor) and persist the bounds are:

public void LoadOptions()
{
// ... open the key

string stringX = (string) keyApp.GetValue("X", "10");
string stringY = (string) keyApp.GetValue("Y", "10");
string stringWidth = (string) keyApp.GetValue("Width", "790");
string stringHeight = (string) keyApp.GetValue("Height", "530");

int intX = Convert.ToInt32(stringX);
int intY = Convert.ToInt32(stringY);
int intWidth = Convert.ToInt32(stringWidth);
int intHeight = Convert.ToInt32(stringHeight);

Bounds = new Rectangle(intX, intY, intWidth, intHeight);
}

public bool PersistOptions()
{
// ... get the key

keyApp.SetValue("X", Bounds.X.ToString());
keyApp.SetValue("Y", Bounds.Y.ToString());
keyApp.SetValue("Width", Bounds.Width.ToString());
keyApp.SetValue("Height", Bounds.Height.ToString());

return true;
}

And my Bounds property:

private Rectangle rectangleBounds;

public Rectangle Bounds
{
get { return rectangleBounds; }
set { rectangleBounds = value; }
}
 
Problem Fixed!

I moved my Form.Bounds setting code out of the Form constructor and into my
Form Load handler and it worked:

private void FormMDIContainer_Load(object sender, System.EventArgs e)
{
Rectangle bounds = Options.Instance.ApplicationOptions.Bounds;
this.Bounds = bounds;
this.Show();
}

So what's the reason?
 
I've loaded form state through the constructor and I've never had a problem.
Although I wasn't using MDI forms either so I'm wondering if it's something
related to that. What have you set the "StartPosition" property of the MDI
container form to be? Have you set this to "Manual"?
 
Yes, I set it to Manual. I used the property sheet in the Form designer to
set this property so it is called in InitializeComponent like so:

this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
 
I'm seeing this behavior now. I didn't have a MainMenu on my Form before but
once I dropped one on an extra 19 pixels (the size of a menu according to
the SystemInformation.MenuHeight value on my system) was added to the Form
height. So it does look like it's related to the MainMenu. Unfortunately, I
don't have a concrete reason for this. I searched through the google
archives and this has come up before. Some one explained the issue and the
link is below. However, like you have already done, it seems to work ok
within a Load event handler.

http://groups.google.ca/group/micro...forms/msg/9a43c8bf559c70a3?dmode=source&hl=en
 
Thanks Tim, I'm confussed by the behavior but I'm satisfied that I have a
solution. Sorry for the misplaced reply on my last post.
 
Back
Top