Saving the "restored" winform size

M

Marc Scheuner

Folks,

I'm writing a component to automatically store a form's size and
position to the registry, and restore it from there. Works fine,
except...

If my form is 300x500 pixel, at location (100,100), and I maximize it,
everything's fine - I can restore it back to "regular" size.

However, if I store the form's location and size into the registry,
while it's maximized, and then close it, next time around, it gets
opened in maximized state as expected. BUT: I can't restore it back to
its pre-maximized size.

Is there any way I can access the size that the form was before being
maximized, and store that into the registry (and restore it from
there), too??

I did see a "restoredWindowBounds" variable inside my form when
debugging - but that appears to be internal, and I can't seem to find
a way to access it.....

Any ideas??

Thanks!
Marc
 
T

Thomas Weingartner

Hi Marc

You can access "any" member using Reflection. I think the Debugger does it the
same way. Please refer to the .NET documentation how to use Reflection.

Good luck
Thomas
 
P

Phil Jenson

Marc,

I don't know if there is a property to do what you want but if all fails you could perhaps keep track of the form size, in the resize event, ignoring any updates if the WindowState property is not set to normal, then you can save these values when the form is closed (assuming they are not zero).


Phil Jenson

--------------------------------------------------------------------------------
 
M

Marc Scheuner [MVP ADSI]

Hi Thomas
You can access "any" member using Reflection. I think the Debugger does it the
same way. Please refer to the .NET documentation how to use Reflection.

I am trying to implement this, and I thought I had a fairly good
understanding of .NET reflection - but I keep seeing a problem....

Here's my code, trying to get a hold of the "restoredWindowBounds"
field in the System.Windows.Forms.Form object which is the base type
of my own form. However, whatever I try to do, the "FieldInfo"
variable always remains at "null" - any ideas why???

private void btnSave_Click(object sender, System.EventArgs e)
{
RegistryKey oRegKey = Registry.CurrentUser.CreateSubKey(<some key>);
if(oRegKey != null)
{
Type oType = this.GetType().BaseType;
FieldInfo oFI = oType.GetField("restoredWindowBounds",
BindingFlags.NonPublic);

oRegKey.Close();
}
}

I assumed that if I get the base type's "Type", I'd be able to get the
FieldInfo for the "restoredWindowBounds" field, and then somehow get
the information of the restored window size from it - but it stays at
null, no matter what I do.

What am I missing??

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
I

Ian Cooper

Hi Marc,
I assumed that if I get the base type's "Type", I'd be able to get the
FieldInfo for the "restoredWindowBounds" field, and then somehow get
the information of the restored window size from it - but it stays at
null, no matter what I do.

ReflectionPermission problems maybe - the GetField documetnation identifies this as a potential cause of being returned null when asking for non-public members.
Ian Cooper
wwww.dnug.org.uk
 

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