views:

32

answers:

0

Hi.

I'm trying to turn some text to Hyperlink while a user is typing in WPF RichTextBox.

My first attempt at it involves running this code at each KeyUp:

Regex r = new Regex("[A-Z]{3}");
FlowDocument doc = this.inputBox.Document;            
String text = new TextRange(doc.ContentStart, doc.ContentEnd).Text;

foreach (Match m in r.Matches(text))
{
    TextPointer start = doc.ContentStart.GetPositionAtOffset(m.Index + 2);                
    TextPointer end = doc.ContentStart.GetPositionAtOffset(m.Index + m.Length + 2);
    Hyperlink sp = new Hyperlink(start, end);
}

This runs correctly the first time a user enters a sequence of 3 capital letters, ABC, but hyperlink creation fails with an exception when a second sequence is entered. Looking at the variable while debugging, it appears that the two TextPointers keep pointing at the first sequence.