Saving and Restoring Form Locations

  • Thread starter Thread starter Chuck
  • Start date Start date
C

Chuck

I want to save the location of my forms when they are closed.

I was thinking of using: UserScopedSettingAttribute class
http://msdn2.microsoft.com/en-us/library/zz7y4375(VS.80).aspx

Had three questions

1.
What's the difference between [UserScopedSetting()] and
[UserScopedSettingAttribute()]

2.
Is there anyway I could incorporate this in my baseForm class and then
override the OnClosing to save the form location for every form that
is inherited from my class. I wasn't sure if I could because it looks
like the UserScopedSettingAttribute class saves the data to a file
user.config:
<userSettings>
<WindowsApplication1.ClassName>
So every form would try to save to the same place (appName.ClassName).

3.
How can I ensure that when I restore a form to the saved form location
the location is still valid/visible. For example I have a two monitor
display with display 2 on the left.
Thus with display 2 and a form in the top left the form.location would
be -1024,0 (If display 2 was on the right the location would be 0,0)

If the user changes the display so 2 is now on the right my
application would try to display it at -1024 which would now be off of
the screen. Is there anyway to test that a location is within the
viewable area?
This happens, I've had applications that I can't get at anymore after
I swapped monitors.

thanks,
 
Hi Chuck,

Thanks for your post.

#1£¬this is language question. As you can see in the following link:
"By convention, all attribute names end with the word "Attribute" to
distinguish them from other items in the .NET Framework. However, you do
not need to specify the attribute suffix when using attributes in code. For
example, [DllImport] is equivalent to [DllImportAttribute], but
DllImportAttribute is the attribute's actual name in the .NET Framework."

"Using Attributes"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vclrfusingattributes.asp

#2, There is no build-in support to store the setting for different forms.
However, we can add a little customization code to get this done, please
refer to the link below:
http://groups.google.com/group/microsoft.public.dotnet.general/browse_thread
/thread/929361aee0e1a1d7/a46aa7eb9a80a39c?lnk=st&q=%22Jeffrey+Tan%22+user.co
nfig&rnum=2&hl=zh-CN#a46aa7eb9a80a39c

#3, no, the OS will only recognize left and right monitor coordinate, so it
will always display regarding the relative left and right, regardless of
which monitor display is on the left.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I think I could use the SetWindowPlacement API call to ensure the
Windows is in a visible area. I wonder if it works for dual monitors.
I was hoping there was a way to do it without interop.
 
On Mon, 20 Feb 2006 07:05:38 GMT, (e-mail address removed) ("Jeffrey
Tan[MSFT]") wrote:

That example doesn't work, the properties have to have the same name
as what you use in the get/set. SettingsPropertyNotFoundException.
 
Hi Chuck,

Thanks for your feedback.

I am not sure I understand you completely. With 2 displays, I do not know
why the original visible stored position becomes an invisible position?
Whatever display is on the left, I do not know why it will affect the
visibility of the Form.

Anyway, I think SetWindowPlacement should work for the visibility issue,
you may give it a test on your scenario.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Lets say you have 2 monitors
#2 on the left and #1 on the right (as set in Display Properties)
your form is displayed on the left monitor and you save the
coordinates (-1024, 0) Note his display goes from -1024 to 1024 and 0
to 768.

The user now gets a new cubicle and moves his computer. He notices
his displays are backward. Being a smart guy he changes the monintor
Display Properties : Setting so that monitor 2 is now on the Right.
Rather than changing the monitor connectors. Note his display now
goes from 0 to 2048 and 0 to 768.

Restart the app and it will display the form at -1024,768 which is now
off screen.
 
Very nice code but it doesn't test for invalid position coordinates
when the form is restored. I have found the typical reasons for
invalid coordinates are :
dual monitor switching
resolution change
font size change.



Hi,

See: http://www.habjansoftware.com/config_library.aspx (Configuration
Library with Layout Saver for .NET)
This component is free and you can download source code of it.


Regards,
Josip Habjan

url: http://www.habjansoftware.com



I want to save the location of my forms when they are closed.

I was thinking of using: UserScopedSettingAttribute class
http://msdn2.microsoft.com/en-us/library/zz7y4375(VS.80).aspx

Had three questions

1.
What's the difference between [UserScopedSetting()] and
[UserScopedSettingAttribute()]

2.
Is there anyway I could incorporate this in my baseForm class and then
override the OnClosing to save the form location for every form that
is inherited from my class. I wasn't sure if I could because it looks
like the UserScopedSettingAttribute class saves the data to a file
user.config:
<userSettings>
<WindowsApplication1.ClassName>
So every form would try to save to the same place (appName.ClassName).

3.
How can I ensure that when I restore a form to the saved form location
the location is still valid/visible. For example I have a two monitor
display with display 2 on the left.
Thus with display 2 and a form in the top left the form.location would
be -1024,0 (If display 2 was on the right the location would be 0,0)

If the user changes the display so 2 is now on the right my
application would try to display it at -1024 which would now be off of
the screen. Is there anyway to test that a location is within the
viewable area?
This happens, I've had applications that I can't get at anymore after
I swapped monitors.

thanks,
 
Hi Chuck ,

Thanks for your feedback.

Oh, yes, it seems the screen system coordinate (0, 0) is always start from
the primary monitor.

Regarding this scenario, I think you may give SetWindowPlacement a test. I
think it should work for you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top