Save / Restore : size and position of the form

  • Thread starter Thread starter Gabriel
  • Start date Start date
G

Gabriel

Hello,

How can I save and restore the position and the size dive by a user and
restore them next time I start my application ?

Thanks,
 
Gabriel said:
Hello,

How can I save and restore the position and the size dive by a user and
restore them next time I start my application ?

Create a string that represents the size/position of your form and save it
to the registry. For maximised forms you can store the size the form would
be if was restored. This is a very useful feature as it can be annoying for
users if they close a form when it's maximised and lose the size they've
setup for their form. If the form is minimised when closed (if you allow
that) then store the normal size of the form, not the maximised. You might
need to use the API to get the restored size if the form is maximised.
 
In Visual Studio 2005, this is much easier than the suggestions you've been
given so far.

Go to the Design view of the Control you wish to save and restore these
properties for, and go to the Properties View of the Control. Find the
"Data" section, and expand the "ApplicationSettings" node. Find the
"PropertyBinding" box and click it, then click on the button with the
ellipse that appears on the right-hand side of it. You will see a list of
all the available bindable properties for the Control. Select the "Position"
property, and click on the box on the right of it. Open the drop-down list
box, and select "new" from the list. You will get a dialog box with 3 items
in it: Default Value, Name, and Scope. Scope will already be set to User,
which is what you want, since only User-Scoped settings may be saved. The
Default Value will be set to the Default value you've already assigned to
that property. Give the Property Setting a name that is easy to identify,
such as "MyControlPosition". Okay your way back out.

You will see that if you open the Project Properties box (right-click the
Project in the Solution, and select "Properties" from the context menu),
there will be a new User-Scoped Setting added, according to your wishes. All
that is left now is to ensure that it gets saved when it is changed. To do
this, just override the OnClosing method of your Form. Add the following
line to the method:

Properties.Settings.Default.Save();

You can easily do this with any Control in the Form.

--
HTH,

Kevin Spencer
Microsoft MVP
Virtual Carpenter
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
 
You will see that if you open the Project Properties box (right-click the
Project in the Solution, and select "Properties" from the context menu),
there will be a new User-Scoped Setting added, according to your wishes.
All that is left now is to ensure that it gets saved when it is changed.
To do this, just override the OnClosing method of your Form. Add the
following line to the method:

Properties.Settings.Default.Save();

You can easily do this with any Control in the Form.

Kevin,

I have datagrid in my form which is dynamically generated from definition in
database.

Users can change grid column order, column widths and colors.
How to save/restore this datagrid settings ?

I think I need a loop over all user-customizable Datagrid settings and
store/restore them to/from isolated storage.
Where to find sample which implements this ?

Andrus.
 
As I explained, you just bind each property to a Project Setting. The code
is all written for you. After that, you can tweak it, save Settings when and
where you wish, etc.

--
HTH,

Kevin Spencer
Microsoft MVP
Virtual Carpenter
http://unclechutney.blogspot.com

Paranoia is a state of mind.
 
Back
Top