2

Is it possible to dynamically add hyperlinks without creating new paragraphs like in this question Dynamically adding hyperlinks to a RichTextBox?

I want something like "Please visit http://www.google.com. Thank you!" not

"Please visit

http://www.google.com

.Thank you!".

Also RichTextBox must be readonly, user cannot type in it. It's something like log, all I need is to periodically add some text which sometimes contains URLs.

Community
  • 1
  • 1
Alex P.
  • 3,697
  • 9
  • 45
  • 110

4 Answers4

10

OK, looks like here is what I need (thanks @Blam and @PaulN Dynamically adding hyperlinks to a RichTextBox):

    public MainWindow()
    {
        InitializeComponent();

        rtb.IsDocumentEnabled = true;
        rtb.Document.Blocks.FirstBlock.Margin = new Thickness(0);
    }

    private void AddHyperlinkText(string linkURL, string linkName, 
              string TextBeforeLink, string TextAfterLink)
    {
        Paragraph para = new Paragraph();
        para.Margin = new Thickness(0); // remove indent between paragraphs

        Hyperlink link = new Hyperlink();
        link.IsEnabled = true;
        link.Inlines.Add(linkName);
        link.NavigateUri = new Uri(linkURL);
        link.RequestNavigate += (sender, args) => Process.Start(args.Uri.ToString()); 

        para.Inlines.Add(new Run("[" + DateTime.Now.ToLongTimeString() + "]: "));
        para.Inlines.Add(TextBeforeLink);
        para.Inlines.Add(link);
        para.Inlines.Add(new Run(TextAfterLink)); 

        rtb.Document.Blocks.Add(para);
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {   
        AddHyperlinkText("http://www.google.com", "http://www.google.com", 
               "Please visit ", ". Thank you! Some veeeeeeeeeery looooooong text.");
    } 

enter image description here

But one little problem left: maybe someone know how to remove blank space at the beginning which is marked with the red line on the image above?

Community
  • 1
  • 1
Alex P.
  • 3,697
  • 9
  • 45
  • 110
  • Have you been able to figure out how to remove that empty line? I am having the same issue :( – BVB Oct 30 '13 at 19:01
  • @BVB, no. I also tried to modify somehow and use solution from this answer http://stackoverflow.com/questions/861409/wpf-making-hyperlinks-clickable but hadn't finished it because abandoned project where I needed that. – Alex P. Nov 02 '13 at 14:08
  • I was able to figure it out, but the solution is a bit hacky. I am not fully sure why it works. `rtb.Document.Blocks.Add(para); rtb.Document.Blocks.Remove(rtb.Document.Blocks.FirstBlock); rtb.Document.Blocks.Add(para);` Adding a paragraph, removing the first block, and then adding the paragraph again got rid of the empty line. Before: http://i.imgur.com/LVWtKkQ.png After: http://i.imgur.com/52MKbwJ.png – BVB Nov 04 '13 at 17:43
  • 1
    `link.IsEnabled = true;` This will make WPF also navigate to said link while `RequestNavigate` will open browser. Set IsEnabled to False prevent WPF from opening link in itself. – kchoi May 20 '16 at 17:56
  • 1
    Small overlooked detail -- This solution also requires `RichtTextBox.IsDocumentEnabled=True`, as mentioned in the page linked in the original question – Zachary Canann May 06 '17 at 07:53
  • In my case the opening of the link does not work. The solution is to change `Process.Start(args.Uri.ToString())` to `System.Diagnostics.Process.Start("cmd","/c start http://www.duckduckgo.com");`. Got the solution from this [answer](https://stackoverflow.com/a/49529618/13624968). – devbf Jan 05 '22 at 12:16
2

As for making a RichTextBox or TextBox read only

TextBoxBase.IsReadOnly Property

For adding text you can use a run

    FlowDocument doc = new FlowDocument();
    rtb.Document = doc;
    rtb.IsReadOnly = true;

    Paragraph para = new Paragraph();
    doc.Blocks.Add(para);

    Hyperlink link = new Hyperlink();
    link.IsEnabled = true;
    link.Inlines.Add("Hyperlink");
    link.NavigateUri = new Uri("http://www.google.co.uk");
    para.Inlines.Add(link);
    Run run = new Run();
    run.Text = " next words";
    para.Inlines.Add(run);
paparazzo
  • 44,497
  • 23
  • 105
  • 176
2

Note: To Remove the Blank Line from the RichText by doing the following:

MyRichTextBox.Document.Blocks.Clear();

move blank space at the beginning of RichTextBox as you add Paragraph Runs

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
1

You can do it with

  <ContentControl>
    <Span>
        <Run Text="Please visit"/>
        <Hyperlink NavigateUri="http://google.com">
            <Run Text="google"/>
        </Hyperlink>
        <Run Text=". Thank you!"/>
    </Span>
</ContentControl>

And if you are in a navigationFrame you get the hyperlink functionality for free

Or...

<StackPanel Orientation="Horizontal">
<TextBlock Text="Please visit"/>
<Button Style="linkButton" Content="Google" Command/Click="GotoGoogle"/>
<TextBlock Text=". Thank you!"/>
</StackPanel>
AlSki
  • 6,868
  • 1
  • 26
  • 39