Dynamically adding hyperlinks to a RichTextBox

后端 未结 2 1789
醉酒成梦
醉酒成梦 2020-12-31 11:30

I\'m trying to dynamically add some hyperlinks to a RichTextBox using WPF and C# but am not having much success. My code is summarised below:

FlowDocument do         


        
相关标签:
2条回答
  • 2020-12-31 12:04

    A simple solution for reading a richTextBox text and transforming it into a link:

    richTextBox.IsDocumentEnabled = true;
    
    TextPointer t1 = richTextBox1.Document.ContentStart;
    TextPointer t2 = richTextBox1.Document.ContentEnd;
    TextRange tr = TextRange(t1,t2);
    string URI = tr.Text;
    
    Hyperlink link = new Hyperlink(t1, t2);
    
    link.IsEnabled = true;
    link.NavigateUri = new Uri(URI); 
    link.RequestNavigate += new RequestNavigateEventHandler(link_RequestNavigate);
    
    
    private void link_RequestNavigate(object sender,RequestNavigateEventArgs e)
    {
        System.Diagnostics.Process.Start(e.Uri.AbsoluteUri.ToString());
    }
    
    0 讨论(0)
  • 2020-12-31 12:14

    The Document in a RichTextBox is disabled by default, set RichtTextBox.IsDocumentEnabled to true.

    0 讨论(0)
提交回复
热议问题