I no longer have a Windows machine and thus no access to VS, but I can hopefully point you in the right direction...
Kyle Biro @ Wed Dec 11, 2013 10:49 pm wrote:
I've created a combo box and I'm trying to add all the installed fonts to it but nothing shows up when I run the program.
What do you mean by "nothing shows up"? Does it add anything to the combo box? Are you able to see a number of blank spaces where the text should be? A picture may be helpful.
I would make use of foreach, rather than for. In this following case you are starting the index at 1 rather than 0.
C#: |
void InstalledFonts ()
{
InstalledFontCollection fonts = new InstalledFontCollection ();
for (int i = 1; i < fonts. Families. Length; i++ )
{
tb_Font. Items. Add(fonts. Families[i ]. Name);
}
}
|
Try running this: C#: |
private void InstalledFonts ()
{
InstalledFontCollection fonts = new InstalledFontCollection ();
foreach (FontFamily family in fonts. Families)
{
Console. WriteLine(family. Name);
}
} |
Does it print a list of fonts to the console? If not, something else is going on. If so, adapt it to use your combobox and see what happens.
I would do the following, avoiding the loop entirely: C#: |
private void InstalledFonts ()
{
InstalledFontCollection fonts = new InstalledFontCollection ();
tb_Font. DataSource = fonts. Families;
tb_Font. DisplayMember = "Name";
} |
MSDN has some helpful information regarding fonts here.
Edit: Oh, forgot to mention. The great thing about using DataSource is you can do something like this to get the FontFamily object back from the list. C#: |
FontFamily selected = (FontFamily)tb_Font.SelectedItem |
.