How to use font dialog in winforms c#

Font dialog is used for changing the font style of the text. In font style there are many attributes available like font face, font color, font size etc. Now, if you want to use font dialog in windows form then simple drag a font dialog control from the tool box and drop into your form page also set some required properties , OK lets take an simple example, in this we take some control on windows form and change their font attribute through font dialog. follow my steps for using font dialog control.



Step-1 : Add one rich textBox and Button control on the form.
Step-2 : Raise click event on button control.
Step-3 : Copy this code and paste into your button control event handler.

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 WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FontDialog ftdlg = new FontDialog();
            ftdlg.ShowColor = true;
            if (ftdlg.ShowDialog ()==DialogResult.OK & !String.IsNullOrEmpty(richTextBox1.Text))
            {
                richTextBox1.SelectionFont = ftdlg.Font;
                richTextBox1.SelectionColor = ftdlg.Color;
            }
        }
    }
}

Now code generate the following output












In this example, first line in event handler define, first to create instance of font dialog, font dialog control doesn't show color in it bydefault so you can change property of it using set ShowColor is true, which is define in second line. In third line we are checking that font dialog is open also return true, when we press OK button. 
Note : Third line also check that 'text' must be selected in rich textBox
 After setting the attribute you can assign attribute to selected text, which is define in fifth and sixth line in the code

Comments

Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites