Export (0) Print
Expand All

How to: Display Web-Style Links with the Windows Forms RichTextBox Control

The Windows Forms RichTextBox control can display Web links as colored and underlined. You can write code that opens a browser window showing the Web site specified in the link text when the link is clicked.

To link to a Web page with the RichTextBox control

  1. Set the Text property to a string that includes a valid URL (for example, "http://www.microsoft.com/").

  2. Make sure the DetectUrls property is set to true (the default).

  3. Create a new global instance of the Process object.

  4. Write an event handler for the LinkClicked event that sends the browser the desired text.

    In the example below, the LinkClicked event opens an instance of Internet Explorer to the URL specified in the Text property of the RichTextBox control. This example assumes a form with a RichTextBox control.

    Security note Security Note

    In calling the Process::Start method, you will encounter a SecurityException exception if you are running the code in a partial-trust context because of insufficient privileges. For more information, see Code Access Security Basics.

       System::Diagnostics::Process ^ p;
       void richTextBox1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkClickedEventArgs ^  e)
          // Call Process.Start method to open a browser
          // with link text as URL.
          p = System::Diagnostics::Process::Start("IExplore.exe",

    (Visual C++) You must initialize processp, which you can do by including the following statement in the constructor of your form:

    p = gcnew System::Diagnostics::Process();

    (Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler.

    this->richTextBox1->LinkClicked += gcnew
       (this, &Form1::richTextBox1_LinkClicked);

    It is important to immediately stop the process you have created once you have finished working with it. Referring to the code presented above, your code to stop the process might look like this:

    public: void StopWebProcess()
© 2015 Microsoft