Saving a Form Size, Location, and WindowState

Posts 13 of 3 · Page 1 of 1
Saving a Form Size, Location, and WindowState
Brief Description
This lets you save and load these main settings(Form Size(Width and Height), Form Location and form WindowState(Maximized or not) for a desired form. Adds professionalism!

Step 1
• Open your Program Properties.
- This can be opened via multiple ways.
○ Project > "Your Project Name" Properties...
○ Double click My Project, located at the top of your Solution Explorer under "Your Project Name"

Step 2
• Go to the Settings Tab and add in these settings
Name: frmWidth Type: Integer Value: 500
Name: frmHeight Type: Integer Value: 500
Name: frmMax Type: Boolean Value: False
Name: frmLocation Type: System.Drawing.Point Value: 0, 0
Note: You can change the Value to anything you want as well as the Name


Step 3
• Now Add in these codes
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If My.Settings.frmMax = True Then
            WindowState = FormWindowState.Maximized
        Else
            Me.Location = My.Settings.frmLocation
            Height = My.Settings.frmHeight
            Width = My.Settings.frmWidth
        End If
    End Sub
Code:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If WindowState = FormWindowState.Maximized Then
            My.Settings.frmMax = True
            My.Settings.frmWidth = 500
            My.Settings.frmHeight = 500
        Else
            My.Settings.frmLocation = Me.Location
            My.Settings.frmMax = False
            My.Settings.frmWidth = Width
            My.Settings.frmHeight = Height
        End If
        My.Settings.Save()
    End Sub
Final Step (Step 4)
• Build your project!
○ Press F5 to Debug
○ Debug > Start Debugging
○ Build > Build "Your Project Name"
You forgot to save the settings at the end of the Form_Closing event. Modified your code.
Thats not needed if you have this checked (In this situation you don't need to add that. If it was a settings menu and you used your settings to change things in your application, then that'll be needed)

Quote Originally Posted by Hassan View Post
You forgot to save the settings at the end of the Form_Closing event. Modified your code.
Posts 13 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?