Monday, January 26, 2015

Saving User Settings In Windows Forms

8:56 PM - By azeheruddin khan 0

In this article, I will show you how to save users form settings like  Form BackColor, ForeColor and BorderStyle. In addition to these you can add other form settings like FormPosition, Last form worked on etc.

Lets start with sample code.

         
using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Drawing;
using System.Globalization;


namespace SaveUsersSettingDetailsExample
{
    public partial class HomeForm : Form
    {
        public HomeForm()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
        }

        private void HomeForm_Load(object sender, System.EventArgs e)
        {
            this.cmbBorderStyle.SelectedIndex = -1;

            string strGetValue = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\CompanyName\SoftwareName", Program.UserName, null);
            if(!string.IsNullOrEmpty(strGetValue))
            {

            string[] str = strGetValue.Split('|');

            this.BackColor = Color.FromArgb(int.Parse(str[0]));
            this.ForeColor = Color.FromArgb(int.Parse(str[1]));           
            this.FormBorderStyle = (FormBorderStyle)Enum.Parse(typeof(FormBorderStyle), str[2], true);
            }
        }

        private void btnBackColor_Click(object sender, System.EventArgs e)
        {
            
            DialogResult result = colorDialog1.ShowDialog();
           
            if (result == DialogResult.OK)
            {                
                this.BackColor = colorDialog1.Color;
            }
        }

        private void btnForeColor_Click(object sender, System.EventArgs e)
        {
            DialogResult result = colorDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                this.ForeColor = colorDialog1.Color;
            }

        }

        private void cmbBorderStyle_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (this.cmbBorderStyle.SelectedIndex != -1)
            {
                this.FormBorderStyle = (FormBorderStyle)Enum.Parse(typeof(FormBorderStyle),(string)this.cmbBorderStyle.SelectedItem, true);
            }
        }

        private void btnSaveSetting_Click(object sender, EventArgs e)
        {
            string strSettings = this.BackColor.ToArgb().ToString() + "|" + this.ForeColor.ToArgb().ToString() + "|" + this.FormBorderStyle.ToString();

            Microsoft.Win32.Registry.SetValue(@"HKEY_CURRENT_USER\Software\CompanyName\SoftwareName", Program.UserName, strSettings, RegistryValueKind.String);

        }
    }
}




OutPut:
      


Link to download Project code given below: -



About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top