remember a forms position on the screen

  • Thread starter Thread starter cj
  • Start date Start date
You are looking at the location property in the properties window. Go to the
very top of the properties window and expand 'Application Settings' then set
the location.
 
Terry,

Now here's a follow up. I have an app that I allow 2 instances of it to
be running on a pc at the same time. Folks usually like to spread them
out so they can see both at the same time. Will these settings allow
each instance to remember where it was? Or will this cause problems?
 
Well, not a big problem. They will both open in the last saved location.
And then they will have to move one. When they close the form, the settings
will be saved. So the last one closed, will be the setting that the next
one(s) use to start with.
 
That's what I thought. As I detect which one is first and name it
instance A and the next one is named instance B perhaps it would work if
I made two entries in the project properties settings tab. Like
AMainFormLocation and BMainFormLocation then, then -- well shucks that
wouldn't help cause I don't actively control the saving of location.
It's no big deal. I've taken care of the programs that bothered me and
those don't. It was just a natural progression to wonder if it would
work for them too.

I'm not sure how all this works anyway. I guess the settings in the
project properties are saved by VB in the registry? Linking the setting
MainFormLocation to the location property of the application must keep
the values in both in sync. Am I on the right track?

As I said they do know which of them is A and which is B. I have first
thing in the form.load

objMutex = New System.Threading.Mutex(False, "AorB")
If objMutex.WaitOne(0, False) = False Then
objMutex.Close()
objMutex = Nothing
instance = "B"
Else
instance = "A"
End If

Well, if you know of an easy way to save location information for them
individually let me know but I don't have too much time to mess with it.

Thanks for all your help.
 
Here is code designed to keep track of multiple form locations/sizes in the
same application. Instead of adding entries in Settings for each of the
forms in the application, you have 2 settings, location and size, and each
form uses its own settings object. You need an ApplySettings() call in the
Form Load event and a SaveSettings() call in the form closing event. In the
settings file have a formsize and a formlocation setting.

change the " Settings.SettingsKey = Me.Name"
to something like Settings.SettingsKey = Me.Name & Instance

I *think* that this will work.

Private ReadOnly Property Settings() As
System.Configuration.ApplicationSettingsBase
Get
If _settings Is Nothing Then
_settings = New My.MySettings
End If
Return _settings
End Get
End Property

Private Sub ApplySettings()
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
If theSettings.FormSize <> Drawing.Size.Empty Then
Me.Size = theSettings.FormSize
End If
If theSettings.FormLocation <> Drawing.Point.Empty Then
Me.Location = theSettings.FormLocation
End If
End Sub

Private Sub SaveSettings()
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)

If Me.WindowState = FormWindowState.Normal Then
theSettings.FormSize = Me.Size
Else
' If the form was maximized or minimized,
' return to the restore state
theSettings.FormSize = Me.RestoreBounds.Size
End If
theSettings.FormLocation = Me.Location

Settings.Save()
End Sub
 
Thanks so much! It'll probably be the first of the week before I can
test it but I really appreciate it you sending me the code. Thanks!
 
Missed one line, need to declare _settings right before the Private Readonly
Property Settings....

Private _settings as My.MySettings
 
Pls help this humble newbie...
This thread looks like what I need but I'm having trouble following it.
Here's what I want to do- If the user moves the application to another place
on the screen, I want the app to appear there the next time it is run.

According to the link-
"I will use the Location property of the My.Settings object. Follow these
steps to access the My.Settings object:"

What is "My.Settings.Object"? and where do I put it?

Thanks
ColsHub
 
Pls help this humble newbie...
This thread looks like what I need but I'm having trouble following it.
Here's what I want to do- If the user moves the application to another place
on the screen, I want the app to appear there the next time it is run.

According to the link-
"I will use the Location property of the My.Settings object. Follow these
steps to access the My.Settings object:"

What is "My.Settings.Object"? and where do I put it?

Thanks
ColsHub

This might sound rude, but did you type in My.Settings?

The Location object you need should be there unless you are using an
old framework version. BTW if that doesn't work, what framework
version are you using?

Thanks,

Seth Rowe [MVP]
 
Seth- no such thing as rude here.
I'm using Frameworks 1.1
I don't get a "settings" tab when I do Project/ Properties. Should I?
I haven't typed in My.Settings because I don't know where to type it in. Is
this part of the code or does it go in the settings tab that I can't find? A
short example would be great. My ProjectName is "Drawing Builder" and the
FormName is "frmMain".

ColsHub
 
I did this in VB 2005. I'm not sure it will work in 2003. And while
I'm very grateful to Terry for the code to allow this to work on
multiple instances of the same app I must admit I never got a chance to
try it. I still might one day.
 
I did this in VB 2005.  I'm not sure it will work in 2003.  And while
I'm very grateful to Terry for the code to allow this to work on
multiple instances of the same app I must admit I never got a chance to
try it.  I still might one day.




- Show quoted text -

cj,

There's a useful article which includes how to save form size and many
other things, you can do the same for other things like last form
location etc.
http://www.devcity.net/Articles/281/1/article.aspx
 
My problem is I don't get the "settings" tab when I open the Projects/
property window. What am I doing wrong?

ColsHub
Framework 1.1
VisualStudio 2003
 
Looks interesting. ColsHub was strying to save screen positions like
Terry helped me with awhile back. I was just pointing out to him that I
had done that in VB 2005 where he seems to be using 2003.

Of course now I'm using 2008 so I'll have to see what changes were made
to that.
 
The whole My. everything was added in VS2005. You can not do this in 2003
(at least not the way I describe).
 
Just a note cj, it's not multiple instances of the same app, it's mutiple
forms in the same app. and does not require you to 'set-up' in advance a
location and size setting for each form in your app, but uses a 'key'
property of the settings object to associate a setting with the form.
 
Seth- no such thing as rude here.

You must not have seen some of discussions I have.... lol
I'm using Frameworks 1.1

As I feared, the My namespace started with the 2.0 framework.
I don't get a "settings" tab when I do Project/ Properties. Should I?
No

I haven't typed in My.Settings because I don't know where to type it in. Is
this part of the code or does it go in the settings tab that I can't find?

Part of the code, if it existed in the 1.1 framework :-(

Basically what you need to do is store the location of the form when
it closes. The most common places are the registry (which is no longer
recommended) and an XML file or some other file type. Then when the
form opens you just grab the values out of the xml file and set the
appropriate values. The My.Settings class just wraps all this
functionality for you, in your case you will need to write the save/
retrieve methods yourself.

Thanks,

Seth Rowe [MVP]
 
My problem is I don't get the "settings" tab when I open the Projects/
property window. What am I doing wrong?

ColsHub
Framework 1.1
VisualStudio 2003

Assuming you use VS2005 or higher then you should have settings on
properties of your app by default.
 
Back
Top