Developed a Custom FontDialog as an alternative to the standard .Net FontDialog. Key advantage of CustomFontDialog is the full control over look and feel as it is open source. Source and binary files are available at sourceforge.
CustomFontDialog avoids ‘This is not a True Type Font’ exception that affects the standard dialog in some cases. Most probably the exception happens when some installed fonts have invalid meta information. This exception cannot be caught using try/catch block because the exception is thrown from the code outside .Net framework. The exact exception message is ‘Only TrueType fonts are supported. This is not a TrueType font.’. CustomFontDialog gets around this problem by loading only True Type fonts. More details about the problem with standard dialog can be found here and here.
Moreover, CustomFontDialog provides easy access to recently used fonts by moving them at the top of the list in ‘Recently Used’ section.
Usage
Following C# code snippet demonstrates how to instantiate and display Custom FontDialog.
CustomFontDialog.FontDialog fontDialog = new CustomFontDialog.FontDialog(); // sets the default font fontDialog.Font = new Font(FontFamily.GenericSansSerif, 10); if (fontDialog.ShowDialog() == DialogResult.OK) { // Font is selected. Get the selected font using 'fontDialog.Font' } else { // FontDialog is canceled by the user // your code here to handle cancel action }
For the CustomFontDialog to retain the recently selected fonts at the top of the list, the dialog should be instantiated only once and ShowDialog function should be called on the same object whenever the dialog needs to be displayed.
To programmatically add Fonts to be ‘Recently Used Fonts’ section, call AddFontToRecentList method.
Limitation: As compared to the standard FontDialog, CustomFontDialog doesn’t support changing text color.
Updates
Version 0.2.0 (31-May-2014)
Following fixes and enhancements are included in this version:
1- In Font List, arrow keys can be used to move between ‘Recently Used’ and ‘All Fonts’ sections.
2- When user starts typing in Font List, the focus shifts to the filter Text box automatically.
3- Whenever CustomFontDialog is displayed, the focus is on Font List by default.
Hi Umar when I click OK button I getting this error “Parameter is not valid” in line “RichTextBox.Font = FonDialog.Font” to get the selected font and its says: fontDialog.Font = {Name = Reference to a non-shared member requires an object reference. Size=16.0}
please help to solve this problem
Hi,
It seems like you are using class name rather than object reference to get the Font property.
Could you share the piece of code where you are using FontDialog?
Solved, It Is Because: tmp.Dispose()
Private Sub UpdateSampleText()
Dim size As Single = If(txtSize.Text “”, Single.Parse(txtSize.Text), 1)
Dim style As FontStyle = If(chbBold.Checked, FontStyle.Bold, FontStyle.Regular)
If chbItalic.Checked Then
style = style Or FontStyle.Italic
End If
If chbStrikeout.Checked Then
style = style Or FontStyle.Strikeout
End If
Dim tmp As Font = lblSampleArab.Font
lblSampleArab.Font = New Font(lstFont.SelectedFontFamily, size, style)
tmp.Dispose() ‘Here Is The Problem, After I Remove This Line, Your Custom FontDialog Work Fine
End Sub
Now I Wonder Why The Dialog Behaviour Is Like If I Call It Using FontDialog.Show() Whereas I Use FontDialog.ShowDialog()
I Mean When The Dialog Shown It Also Show Another Icon In Taskbar
here is the code, Convert From Your Code:
Dim fontDialog As CustomFontDialog.FontDialog = New CustomFontDialog.FontDialog
‘ sets the default font
fontDialog.Font = New Font(FontFamily.GenericSansSerif, 10)
If (fontDialog.ShowDialog = DialogResult.OK) Then
Hi. I was trying to include your dialog in a project of mine in Visual Studio 2022. However, unintended comparisons by reference can be a problem in my code, so the warning CS0252 is an error for me. It was the line “if (lstFont.Items[i] == “Section”) continue;” within public int IndexOf(FontFamily ff). After I realised comparison by reference was exactly what was intended, I replaced the line with “if (lstFont.Items[i].Equals(“Section”)) continue” and all is well. Figured you might wanna know how to avoid that particular compiler warning. Nice work on this custom control. ;’)