0

In the image below I have a task pane in Microsoft Excel with a Rich Text Box (RTB) in it. Within this text box I want to make some of the text into clickable hyperlinks. Now, I know if you put a hyperlink into the RTB you can make it clickable using something like "Process.Start(e.LinkText);" as was answered in one of my previous questions. However i'm not sure how to embed a link into the text so that the user doesn't actually see the hyperlink. For example, I want "google" in the below image to take the user to www.google.com when the user clicks on it. Any tips on how to accomplish this?VSTO Task Pane

Current code to just put some text in the RTB:

 string test_string = "Google";
        

    //Used to set the text in the task pane in real time
            foreach (Control rtbControl in GlobalVars.myUserControl1.Controls)
            {
                if (rtbControl is RichTextBox & rtbControl.Name == "documentResults")
                {
                    rtbControl.Text = test_string;
                } 
             }
Braxvan
  • 67
  • 14

1 Answers1

0

In the case of RichTextBox, you need to use a proper RTF markup where you can specify colors.

string RTFText =
  '{\rtf1 ' +
  '{\colortbl ;\red238\green0\blue0;}' +
  'Lorem ipsum dolor sit amet ' +
  '{\b {\field{\*\fldinst{HYPERLINK "https://www.example.com/" }}' + 
  '{\fldrslt{\cf1 CLICK_HERE\cf0 }}}} ' +
  'consectetur adipiscing elit.}';

You may find the What is the RTF syntax for a hyperlink? and Inno Setup - How to change the color of the hyperlink in RTF text? pages helpful.

If you use separate controls you can set up LinkLabel color-related properties so it may look like on your screenshot:

// Set the color of the links to black, unless the mouse
// is hovering over a link.
linkLabel1.LinkColor = System.Drawing.Color.Black;
linkLabel1.ActiveLinkColor = System.Drawing.Color.Blue;
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Just wanted some clarification because i'm a little lost on this answer. In the first chunk of code, why / how are you putting multiple characters into ... well, a character. Should those be strings using " " instead? Also how do you display this using "rtbControl" in my code example above. Using "rtbControl.Text" will display all of the text in "RTFText" instead of the actual formatted text. I tried "rtbControl.Rtf", but the "Rtf" property doesn't exist for "rtbControl". – Braxvan Jul 29 '20 at 18:38