Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populating listbox with Font Families is slowing down my form

Posted on 2012-03-22
3
Medium Priority
?
247 Views
Last Modified: 2012-06-21
In my form load I have code that populates a dropdownmenuitem with font families.  It slows down my form loading just a tad.  Is there a way to speed that up?
//POPULATE FONT NAMES
            FontFamily[] ffArray = FontFamily.Families;
            foreach (FontFamily ff in ffArray)
            {
                if (ff.IsStyleAvailable(FontStyle.Regular))
                {
                    toolStripDropDownButton1.DropDownItems.Add(ff.Name);
                 }
0
Comment
Question by:pdering
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 980 total points
ID: 37755888
eager load it in a static List<FontFamily> at form load time like this:-

private static List<FontFamily> lstFonts;
public void Form_Load(object sender, EventArgs args)
{
lstFonts=LoadFonts();
}

private IList<FontFamily> LoadFonts()
{
return FontFamily.Families.Where(f=>f.IsStyleAvailable(FontStyle.Regular)).ToList();
}
0
 

Author Comment

by:pdering
ID: 37755973
what am i doing wrong?...

lstFonts = LoadFonts().ToList();
toolStripDropDownButton1.DropDownItems = lstFonts;
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37758544
Try this:-

        private void Form1_Load(object sender, EventArgs e)
        {
            var fonts = LoadFonts();
            fonts.ForEach(f => toolStripDropDownButton1.DropDownItems.Add(f.Name));
        }

        private static List<FontFamily> LoadFonts()
        {
            return FontFamily.Families.Where(f => f.IsStyleAvailable(FontStyle.Regular)).ToList();
        }

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month5 days, 20 hours left to enroll

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question