0

(Since this question has not been getting any answers, I've re-worded it)

In my application, I have a dialog that holds a Rich Text Box, the box is filled with a Tweet gathered from Twitter. Using the tweet entities I format the tweet to have in-line hyperlinks to links in the tweet, mentions, and hashtags. However, the hyperlinks are never positioned correctly; always being 2 or 3 characters too soon and too far.

This is the code I use to set the text in the Rich Text Box:

TweetText.Document.ContentEnd.InsertTextInRun(Status.Text)
Dim FlowDocument As FlowDocument = TweetText.Document
If LinkEntity.Count > 0 Then
            For Each Entity As Entities.TwitterUrlEntity In LinkEntity
                Dim Start As TextPointer = FlowDocument.ContentStart
                Dim StartPosition As TextPointer
                Dim EndPosition As TextPointer
                If Entity.StartIndex = 0 Then
                    StartPosition = Start.GetPositionAtOffset(Entity.StartIndex)
                Else
                    StartPosition = Start.GetPositionAtOffset(Entity.StartIndex)
                End If
                EndPosition = Start.GetPositionAtOffset(Entity.StartIndex + Entity.DisplayUrl.Length, LogicalDirection.Backward)
                Dim h As New Hyperlink(StartPosition, EndPosition)
                AddHandler h.MouseLeftButtonDown, AddressOf Hyperclick_Link
                h.NavigateUri = New Uri(Entity.Url)
                h.Cursor = Cursors.Hand
            Next
        End If
'I have the other entities here, they have very similar code'
TweetText.Document = FlowDocument

This is my Rich Text Box XAML:

<RichTextBox Name="TweetText" Margin="5" FontSize="14" BorderThickness="0" IsReadOnly="True" />

This is the output:

The problem!

The tweet entity has proper indexes for each entity, but I do think the Rich Text Box has hidden characters that are causing this offset.

ecnepsnai
  • 1,882
  • 4
  • 28
  • 56

2 Answers2

1

It is interesting no one answered this one, but I kind of get it because RichTextBoxes are very nasty to use. I'm currently having trouble with one, too.

So, you are right, RichTextBox does use hidden characters, but you shouldn't try to remove them, as they help it work the way it does. You need to count only the character symbols when indexing, not other invisible tags and symbols.

I'm not quite good with VB, but you should be able to go with for loop and increase your index only if YourTextPointer.GetPointerContext(LogicalDirection.Forward) is TextPointerContext.Text, otherwise, you just skip it.

That way your indexes will match the ones in text.

Dejan Maksimovic
  • 507
  • 1
  • 5
  • 23
  • Alright, I'll try that. I'm currently away from a Windows computer but when I get back to one I'll test your theory. Thanks! – ecnepsnai Jul 31 '12 at 00:57
1

After nearly 10 years..

Fine, I'll do it :D

For my purpose, all my text is always going to be in one paragraph, so with this code I check only its contents (it can be modified to check all paragraphs of course):

public static string GetText(this RichTextBox richTextBox)
{
    string textWithoutHiddenSymbols = "";

    Paragraph p = (Paragraph)richTextBox.Document.Blocks.FirstBlock;

    if (p != null)
    {
        InlineCollection inlines = p.Inlines;

        foreach (var inline in inlines)
        {
            textWithoutHiddenSymbols += (inline as Run)?.Text;
        }
    }

    return textWithoutHiddenSymbols;
}
Starwave
  • 2,352
  • 2
  • 23
  • 30