TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# FontDialog Example

This C# tutorial shows how to use the FontDialog control in Windows Forms.

FontDialog presents a font selection dialog box.

It allows the user to select a font from the list of installed fonts. With the FontDialog control, we quickly add the ability for users to select their favorite fonts.

Font

Start. To start using the FontDialog in a program, add a FontDialog instance, and also a Button and a TextBox to an empty Windows Form. Then, double-click on the Button to insert the button1_Click event handler.

Here: In the example code for button1_Click, we call the ShowDialog method on the FontDialog instance.

C# program that opens FontDialog

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void button1_Click(object sender, EventArgs e)
	{
	    // Show the dialog.
	    DialogResult result = fontDialog1.ShowDialog();
	    // See if OK was pressed.
	    if (result == DialogResult.OK)
	    {
		// Get Font.
		Font font = fontDialog1.Font;
		// Set TextBox properties.
		this.textBox1.Text = string.Format("Font is­: {0}", font.Name);
		this.textBox1.Font = font;
	    }
	}
    }
}

You can check the DialogResult returned by ShowDialog for the DialogResult.OK value. Then with the FontDialog, we can access the Font property. We can set a TextBox instance to use that Font instance.

TextBox

 

In the above screenshot, you can see the program that was created in this tutorial. The Button is linked to the button1_Click event handler. The TextBox is then changed to have the Font that was specified in the FontDialog.

FixedPitchOnly. What does the FixedPitchOnly property on the FontDialog do? This changes the list of fonts in the dialog so that only the monospaced ("fixed") ones are shown. Only fonts like Courier New will be present.

ShowColor. You can also change the ShowColor property to true—this adds a small color chooser to the FontDialog. Another option you have is the ColorDialog control, which offers more options.

ColorDialog

Tip: The ShowEffects property can be set to True or False. If you set it to False, the left-hand control group will disappear.

Tip 2: If you disable ShowEffects, the color chooser drop-down will also be hidden.

Summary. The FontDialog control provides a complete font chooser for your C# Windows Forms programs. Because the dialog is built into Windows, its appearance may change depending on the version of Windows.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf