2
\$\begingroup\$

I wrote these extension methods to provide a generic/re-usable method for saving a form's size, position, and window state. What I would primarily like to know is if there is a better way to handle storing the WindowState property. I would also like to know if there are any bugs I might have overlooked, unnecessary code, or ways to make it more readable.

public static void SaveFormSizeAndLocation( this Form form )
{
    try 
    {
        using( RegistryKey key = Application.UserAppDataRegistry.CreateSubKey(form.Name) )
        {
            if( key != null )
            {
                if( form.WindowState == FormWindowState.Normal )
                {
                    key.SetValue("Left",form.Left);
                    key.SetValue("Top",form.Top);
                    key.SetValue("Width",form.Width);
                    key.SetValue("Height",form.Height);
                }
                if( form.ShowInTaskbar )
                {
                    string windowState = Enum.GetName(typeof(FormWindowState),form.WindowState);
                    key.SetValue("WindowState",form.WindowState);
                }
            }
        }
    }
    catch 
    {
        // Party on, Garth!
    }
}
public static void LoadFormSizeAndLocation( this Form form )
{
    try
    {
        using( RegistryKey key = Application.UserAppDataRegistry.OpenSubKey(form.Name) )
        {
            if( key != null )
            {
                form.Left = (int)key.GetValue("Left",form.Left);
                form.Top = (int)key.GetValue("Top",form.Top);
                form.Width = (int)key.GetValue("Width",form.Width);
                form.Height = (int)key.GetValue("Height",form.Height);
                // Move window into visible screen bounds if outside screen bounds (prevent off-screen hidden windows)
                Rectangle screenRect = SystemInformation.VirtualScreen;
                if( form.Left < screenRect.Left )
                    form.Left = screenRect.Left;
                if( form.Top < screenRect.Top )
                    form.Top = screenRect.Top;
                if( form.Right > screenRect.Right )
                    form.Left = screenRect.Right - form.Width;
                if( form.Bottom > screenRect.Bottom )
                    form.Top = screenRect.Bottom - form.Height;
                if( form.ShowInTaskbar )
                {
                    string windowState = Enum.GetName(typeof(FormWindowState),form.WindowState);
                    windowState = key.GetValue("WindowState",windowState).ToString();
                    form.WindowState = (FormWindowState)Enum.Parse(typeof(FormWindowState),windowState);
                }
            }
        }
    }
    catch
    {
        // Party on, Wayne!
    }
}
\$\endgroup\$
1
\$\begingroup\$

Unnecessary code

The windowState variable isn't used in the SaveFormSizeAndLocation method. And I believe you don't need such variable at all, because form.WindowState.ToString() returns exactly the same string.


Suggestions

The SaveFormSizeAndLocation method.
You check the key for null value, and if it is null nothing happens. Maybe it makes sense to omit that check and to handle (or raise) the exception in order to inform user or to save the reason in the application log.

The LoadFormSizeAndLocation method.
You can reduce nesting by inverting the condition:

using (RegistryKey key = Application.UserAppDataRegistry.OpenSubKey(form.Name))
{
    if (key == null)
    {
        return;
    }

    form.Left = (int)key.GetValue("Left",form.Left);
    form.Top = (int)key.GetValue("Top",form.Top);
    ...
}

Naming

I believe that extension methods for the Form class shouldn't contain the word 'Form' in their names. Consider to shorten names: SaveSizeAndLocation and LoadSizeAndLocation.


Styling

It looks unnatural for me:

  • Spaces after ( and before ). I'd prefer to insert spaces before the ( in operators:

    if (x != y)
    

    and to omit spaces in method declarations and calls:

    someMethod(int a, float b, string c);
    someMethod(a, b, c);
    
  • No spaces between method arguments. I'd prefer to add a space after each comma:

    SomeMethod(firstArg, secondArg, thirdArg);
    

    This should improve readability.
    Personally, I cannot easily distinguish between

    SomeMethod(abc,def,ghi); // 3 arguments
    

    and

    SomeMethod(abc.def.ghi); // 1 argument
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.