Broken hyperlinks in RTF file in RichTextBox

I use RichTextBoxto display an RTF file that includes one hyperlink. The link text is not a URL (the target is a valid URL). RTF was created using Word. Both Word and WordPad correctly recognize links (WordPad does not launch links, but shows the corresponding cursor on the hand).

When I load RTF into RichTextBoxlinks, they are displayed correctly formatted (blue and underlined), but instead of behaving like a link, when the cursor moves over the link, it remains an I- LinkClickedray, the event LinkClickedwill not work, and it actually shows the target between the angle brackets after links (this does not seem correct). Since the link text is not a URL, DetectUrlsthis will not help.

Is there a reason why RichTextBoxthese links are not properly handled or a way to make them work as expected?

Here is the code

TipView.Rtf = tips[tipIndex];
// I've also tried TipView.LoadFile, with identical result

To reproduce the problem, create an RTF document with Word (I use 2000) containing one link, the text of which is not a URL, but is intended for a valid URL, and programmatically load the .rtf file into RichTextBox(I use .NET 2.0 in C # Express 2008).

+6
source share
1 answer

To support hyperlinks, you will need the "rich edit" version . RICHEDIT50W

For this:

  • Or use the .NET Framework 4.7, which is originally used RICHEDIT50Win RichTextBox.
  • In older versions of the .NET Framework, you can change RichTextBoxto use RICHEDIT50W:

    public class ExRichText : RichTextBox
    {
        [DllImport("kernel32.dll", EntryPoint = "LoadLibraryW",
                   CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern IntPtr LoadLibraryW(string s_File);
    
        protected override CreateParams CreateParams
        {
            get
            {
                var cp = base.CreateParams;
                LoadLibraryW("MsftEdit.dll");
                cp.ClassName = "RichEdit50W";
                return cp;
            }
        }
    }
    

RichTextBox Selection Highlight RichTextBox - Unicode.

+4

Source: https://habr.com/ru/post/1721446/


All Articles