dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.35k stars 962 forks source link

RichTextbox font family not applied to new text #8902

Open kwyntes opened 1 year ago

kwyntes commented 1 year ago

.NET version

Tested in 7.0, 6.0 and .NET framework 4.8.

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Not that I know of.

Issue description

Setting the Font or SelectionFont property to some font, like new Font("Arial Black", 11.5f, FontStyle.Italic), programtically entered text, either through AppendText(...) or modifying the Text property directly, displays with the chosen font applied. When entering new text however, be it by simply appending at the end, selecting all or part of the text and then typing new text, or typing in between the text already present, will have the expected font size and style applied, but a default font family.

I tried to figure out where new text input is processed in the RichTextBox and TextBoxBase classes, but I couldn't manage to find it. If anyone could point me to the source code for that, please do so!

https://user-images.githubusercontent.com/78665317/227747410-359ce9aa-37c6-4ab6-a3b3-95263a959f81.mp4

Steps to reproduce

var rtb = new RichTextBox
{
  Font /* or SelectionFont, same behaviour */ = new Font("Arial Black", 11.5f, FontStyle.Italic), // could be any font
};

rtb.AppendText("Sample text");
elachlan commented 1 year ago

@JeremyKuhne is usually the go to for richtext I think.

JeremyKuhne commented 1 year ago

@kwyntes SetCharFormatFont is what you'd debug. I haven't really looked at EM_SETCHARFORMAT so I can't offer a lot beyond that. As this is the way it has always been we probably won't be able to look into it deeply, but you're welcome to investigate and capture whatever you find here. We can discuss further when you have more.