I can't assign RichTextBox.SelectionFont

zequion 151 Reputation points

I try to map RichTextBox.Font and RichTextBox.SelectionFont and RichTextBox.SelectionFont doesn't map unit on "Pixel" so font size is different. I have tried different things and nothing works.

System.Windows.Forms.RichTextBox RichTextBox = new System.Windows.Forms.RichTextBox();  
RichTextBox.Font = RichTextBox.SelectionFont = new System.Drawing.Font("Arial", (float)10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);  


An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points

    I think that the authors had a different vision. The font is applied after selecting a text and assigning a value to SelectionFont, not before. For example, you select a text and press a button. The code of the button makes a font and assigns it to SelectionFont. Then the new font becomes visible on the screen. The font will be kept even if you move the caret or select another text.

    In addition, make sure that you assign the font to the visible control, not to an unused object created by new but not added to form.

0 additional answers

Sort by: Most helpful