Solved

Populating listbox with Font Families is slowing down my form

Posted on 2012-03-22
3
238 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 245 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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