locked
Changing the Font Style of Combox Item RRS feed

  • Question

  • Hello. I'm trying fill a combo box with a list of fonts a user can choose from, and apply to text in a text box. I'm able to compile the list; but when I try to apply a font styling to each item which corresponds to the item's font name, I run into a number of problems. Specifically, the line CmbBox.Items.Add(Font.Name); in the foreach loop works. However, I'm not sure if I'm referencing each combobox list item correctly. When I step through the code, after the line CmbBox.Items.Add(Font.Name); in the foreach loop, the following two lines are skipped over, and I get the following error message at the third line down:

    An exception of type 'System.NullReferenceException' occurred in Envelope Printer.exe was not handled in user code.

    Additional information: Object reference not set to an instance of an object.

    If there is a handler for this exception, the program may be safely continued.

        public class Fonts
        {
            public void FillComboBoxList(List<InstalledFont> FontList, ComboBox CmbBox)
            {
                foreach (InstalledFont Font in FontList)
                {
                    CmbBox.Items.Add(Font.Name);
                    var MyComboBoxItem = CmbBox.Items[CmbBox.Items.Count - 1] as ComboBoxItem;
                    var MyStyle = new Style();
                    MyStyle.Setters.Add(new Setter(ComboBoxItem.FontFamilyProperty, new FontFamily(Font.Name)));
                    MyComboBoxItem.Style = MyStyle;
                }
            }
        }

    Can someone suggest a solution to my problem? Thank you.

    Wednesday, October 30, 2013 4:58 PM

Answers

  • Hi,

    the problem is that the comboboxitem is not generated for you when you add a string as item and try to cast the item back as comboboxitem. Try this

    public void FillComboBoxList(List<InstalledFont> FontList, ComboBox CmbBox)
            {
                foreach (var Font in FontList)
                {
                    var MyComboBoxItem = new ComboBoxItem();
                    MyComboBoxItem.Content = Font.Name;
                    MyComboBoxItem.FontFamily = new FontFamily(Font.Name);
    
                    CmbBox.Items.Add(MyComboBoxItem);
                }
            }

    Works on my machine ;-)

    lh

    • Marked as answer by PDoug Wednesday, October 30, 2013 5:26 PM
    Wednesday, October 30, 2013 5:21 PM

All replies

  • Hi,

    the problem is that the comboboxitem is not generated for you when you add a string as item and try to cast the item back as comboboxitem. Try this

    public void FillComboBoxList(List<InstalledFont> FontList, ComboBox CmbBox)
            {
                foreach (var Font in FontList)
                {
                    var MyComboBoxItem = new ComboBoxItem();
                    MyComboBoxItem.Content = Font.Name;
                    MyComboBoxItem.FontFamily = new FontFamily(Font.Name);
    
                    CmbBox.Items.Add(MyComboBoxItem);
                }
            }

    Works on my machine ;-)

    lh

    • Marked as answer by PDoug Wednesday, October 30, 2013 5:26 PM
    Wednesday, October 30, 2013 5:21 PM
  • Brilliant! You saved my life! Thank you!
    Wednesday, October 30, 2013 5:27 PM