winforms icon indicating copy to clipboard operation
winforms copied to clipboard

RichTextBox's font changed if initial Text is Empty

Open victor-wiki opened this issue 2 years ago • 8 comments

.NET version

.net core 3.1

Did it work in .NET Framework?

Yes

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

if the .net framework < 4.7, no issue.

Issue description

if the initial value of Text is Empty, set the font to "Microsoft Sans Serif, 10pt". After application runs up, enter some text to the RichTextBox, then copy the text to office word, the word shows the font is "Calibri, 10". But, if the initial value of Text is not Empty, the font is same as its original.

Steps to reproduce

1.Create a .net core 3.1 winforms project, drag a RichTextBox control to a frame, set the RichTextBox's font to "Microsoft Sans Serif, 10pt", then run the project and enter some text to the RichTextBox, copy the text to office word, the font becomes "Calibri, 10". 2.Same as step 1, except set the Text to not Empty(such as "abc"), then run the project, enter some text and copy the text to office word, it shows "Microsoft Sans Serif, 10", no problem, it's ok. 3.Create a .net framework 4.7 winforms project, do as step 1, the issue as same as that. 4.Create a .net framework 4.6.2 winforms project, do as step 1, the issue disappears.

victor-wiki avatar Sep 26 '22 14:09 victor-wiki

@victor-wiki , can you confirm in .NET framework 4.7+, using option Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl defined here https://learn.microsoft.com/en-us/dotnet/core/compatibility/winforms in app.config file helps here? This is most likely native richtext box issue. @Olina-Zhang , can you try repro this with a MFC app?

dreddy-work avatar Oct 06 '22 22:10 dreddy-work

@dreddy-work , I add the following section to the app.config of .net v4.7.2 framework project, no issue appears.

<runtime>
    <AppContextSwitchOverrides  value="Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl=true" />
</runtime>

It seems that the issue locates in "RichEdit v4.1".

victor-wiki avatar Oct 07 '22 00:10 victor-wiki

@victor-wiki as a workaround you can instantiate the old RichEdit control in your app as follows: https://github.com/dotnet/winforms/blob/ad2bc946dbd343342e05e48174e0461324a5b097/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/RichTextBoxTests.cs#L10812-L10856

RussKie avatar Oct 07 '22 00:10 RussKie

@RussKie , I'm not an expert to do that. The easiest way for me is to set the RichTextBox's font to "Calibri".

victor-wiki avatar Oct 07 '22 01:10 victor-wiki

@victor-wiki , can you confirm in .NET framework 4.7+, using option Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl defined here https://learn.microsoft.com/en-us/dotnet/core/compatibility/winforms in app.config file helps here? This is most likely native richtext box issue. @Olina-Zhang , can you try repro this with a MFC app?

@dreddy-work With a MFC app, when copy to Office word, can't repro this issue. If try to copy to Onenote and Excel, font will show "Calibri, 11". Please check below gif. repro_onenote_excel

Cassie-Li01 avatar Oct 09 '22 07:10 Cassie-Li01

Seems reasonable that we might want to allow users to specify which version of RichEdit control to load in their app. Consider a property or a quirk to load the new one. @dreddy-work can you take a look and give your suggestion?

merriemcgaw avatar Oct 11 '22 23:10 merriemcgaw

❕ Since .NET Core 3.1 there have been fixes put in place to support v4.1, and re-introduction of support forv 2.x/3.x may require extensive regression tests.

RussKie avatar Oct 11 '22 23:10 RussKie

Not specific versions. we never allowed specific versions. Now that we have runtimeconfig, we could add switch to disable current version and fall back to older to get .NET Framework 4.7 behavior.

dreddy-work avatar Oct 12 '22 16:10 dreddy-work

The workaround is similar to what we did in our test resources. To subclass the RichTextBox and load the legacy one instead for your application. We've made enough changes that it would require significant retesting to enable the legacy RichTextBox version in our codebase. We're happy to help you get the workaround in place, and you can also file feedback against Office to indicate the issue. They ultimately are the team that owns the underlying control. Unfortunately, this does not align with our current roadmap and is not something we are likely to change.

merriemcgaw avatar Oct 19 '22 00:10 merriemcgaw