Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with Visual C# Combo Boxes
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kyle Biro




PostPosted: Wed Dec 11, 2013 10:49 pm   Post subject: Help with Visual C# Combo Boxes

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.
I also have another combo box that should add the numbers 10 to 75 to represent font sizes.

I'm fairly new to C# and I'm just stuck with this. Also, please forgive any formatting mistakes I make as I'm new to the forums. Any help would be appreciated.

How I attempt to get the fonts and add them to the combo box.
C#:

void InstalledFonts()
{
    InstalledFontCollection fonts = new InstalledFontCollection();

    for (int i = 1; i < fonts.Families.Length; i++)
    {
        tb_Font.Items.Add(fonts.Families[i].Name);
    }
}


How I try to add the numbers to the combo box
C#:

void FontSize()
 {
    for (int fntSize = 10; fntSize <= 75; fntSize++)
    {
        tb_FontSize.Items.Add(fntSize.ToString());
    }
}

I do have a method that calls InstalledFonts() and FontSize() when the program loads.
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Thu Dec 12, 2013 11:19 am   Post subject: Re: Help with Visual C# Combo Boxes

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
.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: