ARTICLE

Arrange Application Setting in Windows Form Application

Posted by Sudhakar Chaudhary Articles | Windows Forms C# July 15, 2012
Here I am demonstrating how to arrange application settings without a text file or database.
Reader Level:
Download Files:
 

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();
//********************************************************

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts