3

In my application, i want user to select a font, from a list of fonts available in user's system and set that font to richtextbox. I tried ::

    System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
        foreach (FontFamily family in fonts.Families)
        {
           comboBox1.Items.Add(family.Name);
        }

and

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        richTextBox1.Font = new Font(comboBox1.SelectedText, 14);
    }

but it changes the default behaviour of the text. For example: text includes headings in bold and italics. Setting the font with above code transforms the text in the regular form. My question is, to possibly keep the behaviour of the text as before and not to change the complete text in regular form by keeping the bold text in bold and italics in italics.

Tanuj Wadhwa
  • 2,025
  • 9
  • 35
  • 57

2 Answers2

0

There's an option to initialize a font with a font prototype. Try defining a font prototype as a font with micrsoftsansserif, with desired size and style, and then pass the new font as the second parameter. msdn has all the info.

OFRBG
  • 1,653
  • 14
  • 28
0

SelectedText is for the highlighted part of the ComboBox text property. Probably not what you want:

richTextBox1.Font = new Font(comboBox1.Text, 14);

Also, if you just want the highlighted part of the RichTextBox to have the new font:

richTextBox1.SelectionFont = new Font(comboBox1.Text, 14);

If any part of the highlighted text has a mix of bold and italics and different sizes, etc, the RichTextBox does not preserve those properties very well. See How do I maintain RichText formatting (bold/italic/etc) when changing any one element?

Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225