I want to make a combobox in a c# .NET 4.5 Windows Forms application (note: not WPF) that displays all the truetype installed font on the system and that every font is formatted with the font it's representing ("Times" formatted with Times, "Arial" formatted with arial and so on).
How to do this?
Use ComboBox.DrawItem
eventhandler.
public YourForm()
{
InitializeComponent();
ComboBoxFonts.DrawItem += ComboBoxFonts_DrawItem;
ComboBoxFonts.DataSource = System.Drawing.FontFamily.Families.ToList();
}
private void ComboBoxFonts_DrawItem(object sender, DrawItemEventArgs e)
{
var comboBox = (ComboBox)sender;
var fontFamily = (FontFamily)comboBox.Items[e.Index];
var font = new Font(fontFamily, comboBox.Font.SizeInPoints);
e.DrawBackground();
e.Graphics.DrawString(font.Name, font, Brushes.Black, e.Bounds.X, e.Bounds.Y);
}
For using ComboBox.DrawItem
you need set ComboBox.DrawMode = DrawMode.OwnerDrawFixed