I have two forms that are called by the main form. One is named frmEdit, and is invoked by the ShowDialog (modal) method. The other is named frmSearch and is invoked by the Show (non modal) method. In both cases I want to preserve the last window position so each form (class) begins with

Public Class frmEdit (or frmSearch)

    Dim lastx as Integer = -1
    Dim lasty as Integer = -1

in the Shown event handler for each form I have the code

If lastx >= 0 Then
    Me.Left = lastx
    Me.Top = lasty
End If

and in the FormClosing handler I do

lastx = Me.Left
lasty = Me.Top

What I don't understand is why the frmEdit (modal) form remembers its previous position but the frmSearch (non-modal) does not. frmSearch always invokes with lastx and lasty equal to -1. Can someone please explain and suggest a fix?

Member Avatar for Unhnd_Exception

You must be disposing frmSearch and not frmEdit.

You can use my.Settings. In your settings file Create a user variable named FormSearchStartPostion as a point.

In the form load set the location. In the form closing save the location. This will remain saved next time the application is opened.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.FormSearchStartPostion = Me.Location
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim X, Y As Integer
        If My.Settings.FormSearchStartPostion.X >= SystemInformation.WorkingArea.Left Then
            X = My.Settings.FormSearchStartPostion.X
        Else
            X = SystemInformation.WorkingArea.Left
        End If

        If My.Settings.FormSearchStartPostion.Y >= SystemInformation.WorkingArea.Top Then
            Y = My.Settings.FormSearchStartPostion.Y
        Else
            Y = SystemInformation.WorkingArea.Top
        End If

        Me.Location = New Point(X, Y)
    End Sub

If you don't know where the settings file is its under Project / Properties / Settings Tab.


In the above example it checks the systeminformation.Left and Top instead of 0. If you move your task bar to the left of the screen and set the location of the form to 0 it will be underneith the taskbar. This adjusts for the taskbar. I found this out the hard way and had to make an update on app specifically for that.


You may want to check the right and bottom bounds as well.

Thanks for the sample code. I'll plug it in as required. I still don't understand, though, why the two forms are handled differently. I created a simpler example. The main form has the following code under two buttons:

Private Sub btnShowModal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowModal.Click
        frmModal.ShowDialog()
    End Sub

    Private Sub btnShowNonModal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowNonModal.Click
        frmNonModal.Show()
    End Sub

The two forms, frmModal and frmNonModal are as follows:

Public Class frmModal

    Dim lastx As Integer = -1
    Dim lasty As Integer = -1

    Private Sub modal_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        lastx = Me.Left
        lasty = Me.Top
    End Sub

    Private Sub modal_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        If lastx >= 0 Then
            Me.Left = lastx
            Me.Top = lasty
        End If
    End Sub

End Class

Public Class frmNonModal

    Dim lastx As Integer = -1
    Dim lasty As Integer = -1

    Private Sub nonmodal_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        lastx = Me.Left
        lasty = Me.Top
    End Sub

    Private Sub nonmodal_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        If lastx >= 0 Then
            Me.Left = lastx
            Me.Top = lasty
        End If
    End Sub

End Class

As you can see, both forms have the same code, however, only frmModal retains the values of lastx and lasty. Why is this?

Member Avatar for Unhnd_Exception

When you create a modal form with the form.showdialog the form is not disposed when clicking the close button. When you use form.Show the form is disposed when clicking the close button.

So your modeless form is being disposed, hence the reason why the variables are reset.

Straight from the MSDN LIBRARY. The direction writers.

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.


if you type in

frmModal.ShowDialog
frmModal.Dispose

frmModal will no longer remember the settings.

If you don't want the settings stored the next time the app is open then you should create a module named FormSettings and put you location variables in there. The will remember them for the current instance of the app.

commented: Clear and concise explanation. +3
Member Avatar for Unhnd_Exception

And if your wondering why its not disposed and hidden.

When you show a form with .ShowDialog you typically want to get a response back if it was OK, Cancel, Yes , or NO. If Microsoft made this dispose itself when hitting the close button then when you checked if myModalForm.DialogResult = Yes it would crash because the myModalForm would be disposed and you would not be able to get the DialogResult because you can't access a disposed object. Instead its hidden and not disposed so you can get back that result.

When you show a modeless form the form is disposed when clicking the close button.

Hopefully you can understand why now.

Then it was fortunate that the only form that I wanted to get values from after closing was the edit form as this was the only form that retained values after closing. Thanks very much for the explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.