Click here to Skip to main content
15,880,405 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 10.8K   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 

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.