Solved

Populating listbox with Font Families is slowing down my form

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
insert image with its metadata into SQL server Database 3 89
Why Does This Page Not Show Up? 4 24
Server Error 11 47
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

11 Experts available now in Live!

Get 1:1 Help Now