28

I know that richtextboxes can detect links (like http://www.yahoo.com) but is there a way for me to add links to it that looks like text but its a link? Like where you can choose the label of the link? For example instead of it appearing as http://www.yahoo.com it appears as Click here to go to yahoo

edit: forgot, im using windows forms

edit: is there something thats better to use (as in easier to format)?

Oztaco
  • 3,399
  • 11
  • 45
  • 84

6 Answers6

11

Of course it is possible by invoking some WIN32 functionality into your control, but if you are looking for some standard ways, check this post out: Create hyperlink in TextBox control

There are some discussions about different ways of integration.

greetings

Update 1: The best thing is to follow this method: http://msdn.microsoft.com/en-us/library/f591a55w.aspx

because the RichText box controls provides some functionality to "DetectUrls". Then you can handle the clicked links very easy:

this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked);

and you can simple create your own RichTextBox contorl by extending the base class - there you can override the methods you need, for example the DetectUrls.

Community
  • 1
  • 1
Mario Fraiß
  • 1,095
  • 7
  • 15
  • 2
    The question is about showing a link _with a friendly label_ that _hides the underlying URL_, just like the rendered Markdown links in your answer. The first post you link to has no useful answer, and the second one only shows how to create a link _with the raw URL as the label_. – mklement0 Mar 16 '22 at 16:52
10

Here you can find an example of adding a link in rich Textbox by linkLabel:

    LinkLabel link = new LinkLabel();
    link.Text = "something";
    link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
    LinkLabel.Link data = new LinkLabel.Link();
    data.LinkData = @"C:\";
    link.Links.Add(data);
    link.AutoSize = true;
    link.Location =
        this.richTextBox1.GetPositionFromCharIndex(this.richTextBox1.TextLength);
    this.richTextBox1.Controls.Add(link);
    this.richTextBox1.AppendText(link.Text + "   ");
    this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;

And here is the handler:

    private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
    }
Nima Soroush
  • 12,242
  • 4
  • 52
  • 53
  • This doesn't work properly with scrollable a richTextBox. The link hovers over the control... – RvdK Dec 07 '18 at 16:15
  • 2
    Many thanks for the sample code, I managed to make it work even with scrolling. I put `link.Tag = link.Location;` to backup the link position, and then replace its position by `Point p = (Point)l.Tag;` and `l.Top = richTextBox1.GetPositionFromCharIndex(0).Y + p.Y;` on `richTextBox1_VScroll` and `richTextBox1_HScroll`. – Harry Hung Jan 23 '19 at 06:07
6

I found a way which may not be the most elegant, but it's just a few lines of code and does the job. Namely, the idea is to simulate hyperlink appearance by means of font changes, and simulate hyperlink behavior by detecting what the mouse pointer is on.

The code:

public partial class Form1 : Form
{
    private Cursor defaultRichTextBoxCursor = Cursors.Default;
    private const string HOT_TEXT = "click here";
    private bool mouseOnHotText = false;

    // ... Lines skipped (constructor, etc.)

    private void Form1_Load(object sender, EventArgs e)
    {
        // save the right cursor for later
        this.defaultRichTextBoxCursor = richTextBox1.Cursor;

        // Output some sample text, some of which contains
        // the trigger string (HOT_TEXT)
        richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Underline);
        richTextBox1.SelectionColor = Color.Blue;
        // output "click here" with blue underlined font
        richTextBox1.SelectedText = HOT_TEXT + "\n";

        richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Regular);
        richTextBox1.SelectionColor = Color.Black;
        richTextBox1.SelectedText = "Some regular text";
    }

    private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
    {
        int mousePointerCharIndex = richTextBox1.GetCharIndexFromPosition(e.Location);
        int mousePointerLine = richTextBox1.GetLineFromCharIndex(mousePointerCharIndex);
        int firstCharIndexInMousePointerLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine);
        int firstCharIndexInNextLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine + 1);
        if (firstCharIndexInNextLine < 0)
        {
            firstCharIndexInNextLine = richTextBox1.Text.Length;
        }

        // See where the hyperlink starts, as long as it's on the same line
        // over which the mouse is
        int hotTextStartIndex = richTextBox1.Find(
            HOT_TEXT, firstCharIndexInMousePointerLine, firstCharIndexInNextLine, RichTextBoxFinds.NoHighlight);

        if (hotTextStartIndex >= 0 && 
            mousePointerCharIndex >= hotTextStartIndex && mousePointerCharIndex < hotTextStartIndex + HOT_TEXT.Length)
        {
            // Simulate hyperlink behavior
            richTextBox1.Cursor = Cursors.Hand;
            mouseOnHotText = true;
        }
        else
        {
            richTextBox1.Cursor = defaultRichTextBoxCursor;
            mouseOnHotText = false;
        }
        toolStripStatusLabel1.Text = mousePointerCharIndex.ToString();
    }

    private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && mouseOnHotText)
        {
            // Insert your own URL here, to navigate to when "hot text" is clicked
            Process.Start("http://www.google.com");
        }
    }
}

To improve on the code, one could create an elegant way to map multiple "hot text" strings to their own linked URLs (a Dictionary<K, V> maybe). An additional improvement would be to subclass RichTextBox to encapsulate the functionality that's in the code above.

Guido Domenici
  • 5,146
  • 2
  • 28
  • 38
1

Many moons later there is a solution for .NET (Core), as of at least .NET 6.0 (possibly earlier) - for .NET Framework (whose latest and last version is 4.8) you'll still need one of the other solutions here:

The .Rtf property now recognizes RTF-format hyperlinks; e.g., the following renders as:

This is a true RTF hyperlink: Example Link

this.richTextBox1.Rtf = @"{\rtf1 This is a true RTF hyperlink:\line {\field{\*\fldinst HYPERLINK ""https://example.org""}{\fldrslt Example Link}}} }";
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

The standard RichTextBox control (assuming you are using Windows Forms) exposes a rather limited set of features, so unfortunately you will need to do some Win32 interop to achieve that (along the lines of SendMessage(), CFM_LINK, EM_SETCHARFORMAT etc.).

You can find more information on how to do that in this answer here on SO.

Community
  • 1
  • 1
Alan
  • 6,501
  • 1
  • 28
  • 24
-1

richtextbox in years before never needed hyperlink. remove this all together. i used is as smart terminal emulator in my windows forms applications in asynchronous serial communications with external device targets.

remove ilink as is not needed here.

  • This does not address the OP's original question. Also, this answer's grammar makes it hard to understand as well. Try to keep the original question in mind when attempting to help. Answers like this can just bog the search. – TWhite Jun 04 '23 at 14:20