Anyone know how to save and restore window positions in .NET?
Printable View
Anyone know how to save and restore window positions in .NET?
Just store and restore the Location property of the form. Here is an example although there are plenty of different ways of storing the info. I would probably store the location in the app.config file if I was going to actually do this, but serialization was an easier example.
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If IO.File.Exists("form.config") Then Dim xs As New Xml.Serialization.XmlSerializer(GetType(Point)) Dim fs As New IO.FileStream("form.config", IO.FileMode.OpenOrCreate) Dim pt As Point = xs.Deserialize(fs) fs.Close() Me.Location = pt End If End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Dim xs As New Xml.Serialization.XmlSerializer(GetType(Point)) Dim fs As New IO.FileStream("form.config", IO.FileMode.OpenOrCreate) xs.Serialize(fs, Me.Location) fs.Close() End Sub
Thanks again... you keep kick starting me (haha). This is the concept that I was looking for. I had been storing the info in the registry using VB6, but I'm having trouble converting my code. Then I found this:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'Set window state and position
WindowState = FormWindowState.Normal
StartPosition = FormStartPosition.CenterScreen
And this sets a starting position, but not based on the last used. Now I can get it a little closer.
Thanks.