Click here to Skip to main content
15,904,416 members
Articles / Programming Languages / C#
Tip/Trick

Simple class to save your Form's Size To Registry

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
22 Apr 2011CPOL 11K   6   3
Simple class to save your Form's Size To Registry
C#
using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Chico.Registry
{
    public class SizeRegistry
    {
        private RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
        private RegistryKey mKey;
        public SizeRegistry(string subkey)
        {
            mKey = key.CreateSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.Volatile);
            mKey.OpenSubKey(subkey);
            key.Close(); key.Dispose();
        }
        public void GetSize(out int width, out int height)
        {            
            width = (int)mKey.GetValue("FormWidth",320);
            height = (int)mKey.GetValue("FormHeight", 240);           
        }
        public void SetSize(int FormWidth, int FormHeight)
        {           
            mKey.SetValue("FormWidth", FormWidth, RegistryValueKind.DWord);
            mKey.SetValue("FormHeight", FormHeight, RegistryValueKind.DWord);            
        }
        public void Close()
        {
            mKey.Close();           
            mKey.Dispose();            
        }
    }
}


Here is an example of using this class:

C#
using System;
using System.Drawing;
using System.Windows.Forms;
using Chico.Registry;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
       SizeRegistry key = new SizeRegistry(Application.ProductName);
        int width, height;
        public Form1()
        {
            InitializeComponent();
            key.GetSize(out width, out height);
            this.Size = new Size(width, height);            
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            key.SetSize(this.Width, this.Height);
            key.Close();
        }                               
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralWhat about forms other than the application form? Pin
Marc Clifton22-Apr-11 17:52
mvaMarc Clifton22-Apr-11 17:52 
GeneralRe: What about forms other than the application form? Pin
charles henington2-May-11 2:06
charles henington2-May-11 2:06 
General[My vote of 2] My thoughts... Pin
kornman0022-Apr-11 7:25
kornman0022-Apr-11 7:25 
While simple, this solution is platform specific to Windows. A better solution (yes, I understand this is suppose to be a "simple" class), at least in terms of maintainability and platform independence, would be to have a system that could save individual form size (positions, etc) data and then have a "transport" layer to stream that data to an XML, binary, etc file. The manager class itself could still be simple, but the underlying code wouldn't.

Also, you should roll with an IDisposable pattern since you're dealing with such underlying objects.

I also don't see the point of making the "key" object a member instead of an local object in the ctor (since that's where it's used and disposed) and wrap it all up in a simple using(...) block.

edit: Also, since this is more of a startup/shutdown construct, you could just make a simple class (ie, static) function which takes a control and a boolean or enum value stating what the current state is (opening/closing; and of course a name parameter for the subkey) and having the function handle the get/set, all why keeping the SizeRegistry object as a local object in the function in a using(...) block. This way you're not keeping yet another object around unnecessarily as a member object (since it doesn't/can't do anything else).

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.