I want to replace a regular link with a hyperlink in my RichTextBox, all of the information is given to me via a outside library (LinkEntity). But I don’t know how to actually go about replacing the text.
I have this, but it does not work right, sometimes it throws a value cannot be null exception, and sometimes it just replaces the wrong part of the link.
Dim Start As TextPointer = TweetText.Document.ContentStart
Dim StartPos As TextPointer = Start.GetPositionAtOffset(LinkEntity.StartIndex)
Dim EndPos As TextPointer = Start.GetPositionAtOffset(LinkEntity.EndIndex)
Dim h As New Hyperlink(StartPos, EndPos)
AddHandler h.MouseLeftButtonDown, AddressOf Hyperclick_Link
h.NavigateUri = New Uri(LinkEntity.Url)
h.Cursor = Cursors.Hand
The exception throws because StartPos and EndPos are nothing, even though the Start and End index have a valid value.
I found the problem, TweetText.Document did not have a value when I was calling it when declaring the StartPos and EndPos. My Final code looks like: