articles

Home / DeveloperSection / Articles / Maintain font and font color state in C# .net winform

Maintain font and font color state in C# .net winform

Vijay Shukla 8177 06-Jun-2013

In this article I am trying to explain how to maintain the font and font color state in C# .net winform using settings.

Introduction

The .NET Framework allows you to create and access values that are maintain between application execution sessions. These values are called settings. Below is describe how to create a setting which is maintain values between execution seesions.

Creating a New Setting:

You can create a new setting at design time by using the settings designer. The settings designer as like grid-style interface that allows you to create new settings.

To Create a New Setting at Design Time

1.     In Solution Explorer, expand the Properties of your project.

2.    In Solution Explorer, double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.

3.    In the Settings designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single setting. Below Figure shows an example of the Settings designer.

Maintain font and font color state in C# .net winform

Now I will going to make a demo which is maintain the font style and font color in our windows application.

Below is an image of demo:-

Maintain font and font color state in C# .net winform

In above image have to buttons one for show the dialog box of Font and second for show the Font Color and also have a panel and label where in panel show the selected color and in label show the selected font. After that I will get the both buttons click event for write the code.

Now I will take first Font Style button event:-
FontDialog _fontDailog = new FontDialog();
 private void btnFontStyle_Click(object sender, EventArgs e)
{
. if (_fontDailog.ShowDialog() == DialogResult.OK) 
 {
lblFontStyle.Font = _fontDailog.Font; 
 }
}
 Code Description:-

1.       Create an object for FontDialogwith _fontDailog name.

2.       This is click event method for Font Style button.

3.       Start the method body wit {.

4.       Check the DialogResult.OK

5.       Start the if condition body.

6.       Set the font in lblFontStyle form _fontDailog.Font

7.       Close the if condition body.

8.       Close the click event method body.

Now I will take the Font Color button events:-
 ColorDialog _colorDialog = new ColorDialog();
private void btnFontColor_Click(object sender, EventArgs e)
{
if (_colorDialog.ShowDialog() == DialogResult.OK)
{
panelFontColor.BackColor = _colorDialog.Color;
}
}
Code Description:-

1.       Create an object for FontDialogwith _fontDailog name.

2.       This is click event method for Font Color button.

3.       Start the method body wit {.

4.       Check the DialogResult.OK

5.       Start if condition body {.

6.       Set the font color in panelFontColor form _colorDailog.Color

7.       Close if condition body }.

8.       Close the click event method body.

Now this demo work perfect but after selecting color and font and restart this demo you will see the color and font will reset. For resolve this problem we can create settings below I am going to create, for save the font style and font color state I am going to make a setting.

1.     Go to solution explorer and double click on the properties now you will show a grid-style interface where we declare a variable.

Maintain font and font color state in C# .net winform

2.   In above image I created two name fontColor and fontStyle with System.Drawing.Color for fontColor with default value white color and System.Drawing.Font for fontStyle with default value  font family: “Microsoft Sans Serif ” font size: “9.75pt” and style: “Bold”.

3.       We just add a line in both buttons event:

In Font Color Button Event:
  Properties.Settings.Default.fontColor = _colorDialog.Color;
  Properties.Settings.Default.Save();

In Font Style Button Event:

  Properties.Settings.Default.fontStyle = _fontDailog.Font;

  Properties.Settings.Default.Save();

4.       In above lines of code save the font color in Properties.Settings.Default.fontColor from color dialog box selection and save the font style in Properties.Settings.Default.fontStyle.

5.       And in the last get the form load event and write the below code for set the font color and font style after restarting demo.

private void Form1_Load(object sender, EventArgs e)
        {
            lblFontStyle.Font = Properties.Settings.Default.fontStyle;
            panelFontColor.BackColor = Properties.Settings.Default.fontColor;

        }

 


c# c# 
Updated 07-Sep-2019

Leave Comment

Comments

Liked By