How to Use FontDialog in C# Change Style

You may familiar with FontDialog to change font styles like font size, font family, font style etc in different software like MS Word. Like this you can also use FontDialog in C# to edit font styles.


How to declare a FontDialog:

You can add FontDialog from your C# ‘Tool Box’.
Or type FontDialog fd = new FontDialog();
- Where fd is the name of your FontDialog.

How to open FontDialog:

Type type: fd.ShowDialog();
- Where fd is the name of your FontDialog.

How to get selected font style to your C# program:

‘DialogResult.OK’ is the selected font style when you press on OK button. Like this you can set what happen when you click on Cancel button in your FontDialog.

C# code to change font style of a text in a textbox using FontDialog:


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

        private void button1_Click_1(object sender, EventArgs e)
        {
           FontDialog fd = new FontDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Font = fd.Font;
            }
        }

    }
}


Sample Output:

How to Use FontDialog in C# Change Style

Learn about how to use 'FontDialog' here




No comments:

Post a Comment