Solved

Populating listbox with Font Families is slowing down my form

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now