Description
Windows forms
- Creating properties in the application settings LocationX, LocationY, WindowWidth, WindowHeight (type int)
- Save location and size in
Form_FormClosed - Load and apply layout and size in
Form_Load
Example
private void Form1_Load(object sender, EventArgs e) { this.Location = new Point(Properties.Settings.Default.LocationX, Properties.Settings.Default.LocationY); this.Width = Properties.Settings.Default.WindowWidth; this.Height = Properties.Settings.Default.WindowHeight; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Properties.Settings.Default.LocationX = this.Location.X; Properties.Settings.Default.LocationY = this.Location.Y; Properties.Settings.Default.WindowWidth = this.Width; Properties.Settings.Default.WindowHeight = this.Height; Properties.Settings.Default.Save(); }
Additional Information
WPF
- Creating properties in the application settings LocationX, LocationY, WindowWidth, WindowHeight (type double)
- Save location and size in
MainWindow_Closed - Load and apply layout and size in
MainWindow_Loaded
Example
void MainWindow_Loaded(object sender, RoutedEventArgs e) { this.Left = Properties.Settings.Default.LocationX; this.Top = Properties.Settings.Default.LocationY; this.Width = Properties.Settings.Default.WindowWidth; this.Height = Properties.Settings.Default.WindowHeight; } void MainWindow_Closed(object sender, EventArgs e) { Properties.Settings.Default.LocationX = this.Left; Properties.Settings.Default.LocationY = this.Top; Properties.Settings.Default.WindowWidth = this.Width; Properties.Settings.Default.WindowHeight = this.Height; Properties.Settings.Default.Save(); }
Additional Information
I tested both WinForms and WPF.
source share