Click here to Skip to main content
Click here to Skip to main content

Tagged as

Simple class to save your Form's Size To Registry

, 22 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple class to save your Form's Size To Registry
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:
 
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)

Share

About the Author

charles henington

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? PinprotectorMarc Clifton22-Apr-11 17:52 
GeneralRe: What about forms other than the application form? Pinmembercharles henington2-May-11 2:06 
General[My vote of 2] My thoughts... Pinmemberkornman0022-Apr-11 7:25 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.1 | Last Updated 22 Apr 2011
Article Copyright 2011 by charles henington
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid