RichTextBox sometimes does not assign Font in GraphicsUnit.Pixel

zequion 151 Reputation points
2022-09-02T05:01:59.193+00:00

In WinForms, I create a Font System.Drawing.Font using System.Drawing.GraphicsUnit.Pixel and assign it to a RichTextBox.Font or RichTextBox.SelectionFont control

As I am now working more with this command, I have noticed that sometimes when running MyRichTextBox.Font = MyFont; assigns all values except GraphicsUnit.Pixel and assigns GraphicsUnit.Point

I've been testing this problem for several days and I can't find any sense in it.

In addition, I am seeing that RichTextBox.SelectionFont only uses points even if pixels are indicated. I show it in this example.
According to Microsoft: "If no text is currently selected, the font specified in this property is applied to the current insertion point and to all text that is typed into the control after the insertion point." So it should always be assignable even if no text is selected.

I need SelectionFont to use Pixels as units because I use a combobox that shows the size of the text in the place where the cursor is located and since SelectionFont uses points, it shows the value wrong and then I should use a function that converts points to pixels and I prefer not do it.

Can someone look at the RichTextBox source code to find out why? I've looked a bit but as they constantly use the SendMessage command I don't know anything. Why use that command?

ThRControl_ = new System.Windows.Forms.RichTextBox();  
  
System.Drawing.Font MyFont  = new System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.GraphicsUnit.Pixel);  
System.Drawing.Font MyFont1 = new System.Drawing.Font("Arial", 22, System.Drawing.GraphicsUnit.Pixel);  
  
ThRControl_.Font = MyFont;  
ThRControl_.SelectionFont = MyFont1;  
C#
C#
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,233 questions
{count} votes