Arrange Application Setting in Windows Form Application

You can save and retrieve application settings like page background color, font style and so on.

Form Design:

Goal:

  1. Change Background Color of form
  2. Change Background color of Textbox
  3. Save User ID
  4. Save Password

ApplicationSetting.gif

After designing the form go to the Project Properties.

In the settings tab define a name and the type of a variable, also the scope and provide a default value:

Aloes value can blank

ApplicationSetting1.gif

You can also do this using the app setting:

ApplicationSetting2.gif

Reading Setting

On form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace corner
{
    public partial class Form1 : Form
    {
        //decalre variable***************
        Color FormColor;
        Color TextBoxColor;
        string UserID;
        string Password;

        //****************************** 
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //reading Setting*****************************************
            FormColor = corner.Properties.Settings.Default.FormBg;
            TextBoxColor = corner.Properties.Settings.Default.TextBg;
            UserID = corner.Properties.Settings.Default.UserID;
            Password = corner.Properties.Settings.Default.Password;

            //********************************************************
 
            //Applying Stting*****************************************
            this.BackColor = FormColor;
            textBox1.BackColor = TextBoxColor;
            textBox2.BackColor = TextBoxColor;
            textBox1.Text = UserID;
            textBox2.Text = Password;
            //********************************************************
        }
    }
}

Save Setting:

//Save Setting********************************************
corner.Properties.Settings.Default.FormBg = this.BackColor;
corner.Properties.Settings.Default.TextBg=textBox1.BackColor;
corner.Properties.Settings.Default.UserID=textBox1.Text;
corner.Properties.Settings.Default.Password=textBox2.Text;
corner.Properties.Settings.Default.Save();
//********************************************************

Up Next
    Ebook Download
    View all
    Learn
    View all