I am trying to save the position of a custom dialog to the users registry, so that when they reload the same dialog, it appears in the same place they moved or resized it to pre
You want something like this when you save the window position:
if (this.WindowState == WindowState.Normal)
{
Properties.Settings.Default.Top = Top;
Properties.Settings.Default.Left = Left;
Properties.Settings.Default.Height = Height;
Properties.Settings.Default.Width = Width;
}
else
{
Properties.Settings.Default.Top = RestoreBounds.Top;
Properties.Settings.Default.Left = RestoreBounds.Left;
Properties.Settings.Default.Height = RestoreBounds.Height;
Properties.Settings.Default.Width = RestoreBounds.Width;
// Check for WindowState.Maximized or WindowState.Minimized if you
// need to do something different for each case (e.g. store if application
// was Maximized
}
The important bit is the RestoreBounds
which you need when the window is maximised or minimised. The code can probably be refactored to make it more efficient, but you get the idea.
I guess you are updating the window position when the window is closed? There are a couple of solutions if that is the case.
1) Save the window position on a different event, like when the window is resized or moved. 2) Check to see if the window is minimized before saving the X and Y positions.
Example:
switch (this.WindowState)
{
case WindowState.Maximized:
// don't update the X,Y
break;
case WindowState.Minimized:
// don't update the X,Y
break;
case WindowState.Normal:
// DO update the X,Y
break;
}