Click here to Skip to main content
15,947,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i want create simple editor with richtextbox in C# windows form. in my project users should can add hyperlink for file in the richtextbox. but I have 2 problem that one of the is very weird and strange.

1. my user can type both ansi and utf-8 character, but when the user add hyper link in link text is in utf8, text turn to something like this "????"

2. when user add hyperlink, When the mouse goes over the hyperlink, the pointer turns into a hand; But when we click, the link click event does not firing and worked. the weird point is: when the user click on enter, go to the next line and type at least the same length of the above line. After this, by clicking on the hyperlink, the event will be executed and the file will be opened.
what Couse this problems and how to solve them?

What I have tried:

my code for add hyperlink button is:
private void AddLinkBtn_Click(object sender, EventArgs e)

        string url = null;

        OpenFileDialog openFileDialog = new OpenFileDialog();
        //openFileDialog.Filter = "Image Files (*.jpg;*.jpeg,*.png)|*.JPG;*.JPEG;*.PNG";
        openFileDialog.Multiselect = false;

        if (openFileDialog.ShowDialog() == DialogResult.OK)
            url = openFileDialog.FileName;

        if (!string.IsNullOrEmpty(url))

            string linkText = TextBoxDefinition.SelectedText;
            byte[] bytes = Encoding.UTF8.GetBytes(linkText);
            linkText = Encoding.UTF8.GetString(bytes);

            // Create the hyperlink text in RTF format
            string hyperlinkRtf = @"{\rtf1\ansi{\field{\*\fldinst HYPERLINK ""file://" + url + @"""}{\fldrslt "  + linkText + @"}}}";

            // Set the text of the TextBox to display the hyperlink
            TextBoxDefinition.SelectedRtf = hyperlinkRtf;

and the link click event is :
private void TextBoxDefinition_LinkClicked(object sender, LinkClickedEventArgs e)
        string filePath = e.LinkText.Substring(7);
[no name] 27-Apr-23 12:08pm    
Your extra typing that "works" (probably) means that you've created an "acceptable" document at that point (carriage return or whatever). "Rich" text means it supports multiple fonts; you need to switch fonts if you start getting "???".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900