Reputation:
Here's my code:
private void DialogFont_Load(object sender, EventArgs e)
{
LoadInstalledFonts();
}
private void LoadInstalledFonts()
{
var fontCollection = new System.Drawing.Text.InstalledFontCollection();
foreach (var font in fontCollection.Families)
{
lstFonts.Items.Add(font.Name);
}
}
How can I display each font using its own design, sort of like a preview of the fonts? I'm using the ListBox control to list the fonts.
Upvotes: 2
Views: 2327
Reputation: 6406
First thing, I want to make sure that you are aware of the FontDialog control, and you are purposely wanting to create a custom Font Dialog. If you weren't aware of it, then maybe you can take a look at it and make sure that it doesn't fit your needs before trying to create your own. And the following Stack Overflow question shows you how to make sure it is populated with all the device fonts and not just TrueType fonts.
Now, if you still want to create your own, then the following is a simplified solution:
Add a Label to your Font Dialog and set its text to whatever you want the user to see as a sample of the font. Something like AabBcC
, or it could even be a random sentence.
You can set the Font of the label in the SelectedIndexChanged event of your ListBox. This in effect changes the sample text to match the font you specify. The following is a simple example:
Note that you can also use a Textbox if you want your user to specify the text that they want to see in another font. Also, some fonts like Andy and Aharomi throw an ArgumentException stating that the Font doesn't support a regular style, so it would be wise to catch this exception type.
private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
{
lblSample.Font = new Font(lstFonts.SelectedItem.ToString(), 12);
}
Upvotes: 1
Reputation: 128407
Maybe go with a ListView
instead of a ListBox
? The ListViewItem
type has a Font
property you could use. I'm not aware of any special per-item formatting capabilities of ListBox
.
Update: In case you're still working on this, here's a snippet of some code that worked for me (this won't compile as-is as it's just a clip from a larger user control; I'm sure you can figure out what goes where though):
private void PopulateListView(IEnumerable<FontFamily> fontFamilies)
{
try
{
m_listView.BeginUpdate();
float fontSize = m_listView.Font.Size;
Color foreColor = m_listView.ForeColor;
Color backColor = m_listView.BackColor;
string sampleText = m_sampleText;
foreach (FontFamily fontFamily in fontFamilies)
{
var listViewItem = new ListViewItem(fontFamily.Name)
{
UseItemStyleForSubItems = false
};
var sampleSubItem = new ListViewItem.ListViewSubItem(listViewItem, sampleText, foreColor, backColor, new Font(fontFamily, fontSize));
listViewItem.SubItems.Add(sampleSubItem);
m_listView.Items.Add(listViewItem);
}
}
finally
{
m_listView.EndUpdate();
}
}
Here's what the result looks like:
Upvotes: 1
Reputation: 442
You can do it easily in WPF.
The XAML would look like:
<ComboBox Width="100" Height="30" x:Name="FontSelector">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" FontFamily="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And C# codebehind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FontSelector.ItemsSource = Fonts.SystemFontFamilies;
}
You could also check this article over on CodeProject which walks through (in a Winforms example) how to populate a list box with fonts like you want: http://www.codeproject.com/KB/selection/FontListBoxAndCombo.aspx
Upvotes: 1
Reputation: 67335
Using a ListBox, I would think you'd need to do owner-draw. When drawing each list item, you'd need to select the font for that item.
Upvotes: 0