Home All Groups Group Topic Archive Search About

remember a forms position on the screen

Author
29 Nov 2007 4:40 PM
cj
I was googling for a way to have my single form application start in the
same location on the screen it was in when it was closed.  I found this
very short article:

http://blogs.techrepublic.com.com/programming-and-development/?p=540

I can create the my.settings object as specified but I can't set the
form1's location to mainformlocation in form1's properties.  It takes an
X and Y location.

Help!

Author
29 Nov 2007 5:18 PM
Terry
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


Show quoteHide quote
"cj" wrote:

> I was googling for a way to have my single form application start in the
> same location on the screen it was in when it was closed.  I found this
> very short article:
>
> http://blogs.techrepublic.com.com/programming-and-development/?p=540
>
> I can create the my.settings object as specified but I can't set the
> form1's location to mainformlocation in form1's properties.  It takes an
> X and Y location.
>
> Help!
>
Are all your drivers up to date? click for free checkup

Author
29 Nov 2007 6:30 PM
cj
Wow!, I'd never seen that before.  Thanks!  And the article works.

Terry wrote:
Show quoteHide quote
> 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.
Author
29 Nov 2007 7:00 PM
cj
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?

Terry wrote:
Show quoteHide quote
> 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.
Author
29 Nov 2007 7:46 PM
Terry
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. 
--
Terry


Show quoteHide quote
"cj" wrote:

> 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?
>
> Terry wrote:
> > 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.
>
Author
29 Nov 2007 8:41 PM
cj
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.



Terry wrote:
Show quoteHide quote
> 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.
Author
29 Nov 2007 9:29 PM
Terry
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


--
Terry
Author
29 Nov 2007 9:41 PM
cj
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!

Terry wrote:
Show quoteHide quote
> 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
>
>

Bookmark and Share