0

I'm trying to insert links into my RichTextBox. I do not mean setting DetectUrls to true, I want alternate text. What I have so far seems to be working mostly fine.. I'm using much of the code from this solution. But my issue is that there is some trailing whitespace in the LinkLabel that end up cutting off some of the text that follows it.

Here is what I have so far:

private void Form1_Load(object sender, EventArgs e)
{
    //My text with a placeholder for the link: %link1%
    //NOTE: I can see the leading '>' fine, but the '<' gets hidden by the link label, and it looks like a space between the link text and "Near..."
    richTextBox1.Text = "This has a link here:>%link1%<Near the middle.";

    LinkLabel link1 = new LinkLabel();
    //link1.Margin = new Padding(0); //Doesn't help
    //link1.Padding = new Padding(0); //Default is already 0
    //What I want to see in my hyperlink
    link1.Text = "My_Link_Text";
    link1.Font = richTextBox1.Font;
    link1.LinkColor = Color.DodgerBlue;
    link1.LinkClicked += Link_LinkClicked;
    LinkLabel.Link data = new LinkLabel.Link();
    //For now, just test with google.com...
    data.LinkData = @"http://google.com";
    link1.Links.Add(data);
    link1.AutoSize = true;
    link1.Location = this.richTextBox1.GetPositionFromCharIndex(richTextBox1.Text.IndexOf("%link1%"));
    richTextBox1.Controls.Add(link1);
    //Replace the placeholder with the text I want to see so that the link gets placed over this text
    richTextBox1.Text = richTextBox1.Text.Replace("%link1%", link1.Text);

    //Attempt to manually shrink the link by a few pixels (doesn't work)
    //NOTE: Turns out this cuts back on the link test, but leave the trailing space :(
    //Size s = new Size(link1.Width, link1.Height); //Remember the autosized size (it did the "hard" work)
    //link1.AutoSize = false; //Turn off autosize
    //link1.Width = s.Width - 5;
    //link1.Height = s.Height;
}

The LinkClicked event, just for the curious. Nothing special.

private void Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}

Here is what I see, for reference (notice the missing '<'):

enter image description here

The reason for using a RichTextBox is that I also plan on adding formatting when I am finished (ability for text to be bold, colored, etc.). I'm basically 2 hours away from scrapping the RichTextBox and drawing everything manually onto a panel and handling click position...

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • Can you explain why you are avoiding the DetectUrls? Embedding a link label inside a RichTextBox seems like an odd way to do this. – LarsTech Jul 21 '17 at 16:10
  • @LarsTech - It's not that I'm avoiding DetectUrls, but I don't necessarily want to show a user the actual link in favor of text. For example, similar to SO, where if I want to click on your username, the link says "LarsTech" and not "https://stackoverflow.com/users/719186/larstech" – Broots Waymb Jul 21 '17 at 16:12

0 Answers0