I have the below code attached to a SelectionChanged event in a WPF RichTextBox. The idea is for it to select the sentence the user is currently editing, a la iAWriter. Any ideas why it only selects past the cursor, or occasionally throws a ArgumentNullException? Thanks in advance.
Regex sentence = new Regex(@"(([.?!][^\s])*[^.?!])*[.?!]\s");
Regex srtl = new Regex(@"(?:[.?!]\s)(([.?!][^\s])*[^.?!])*", RegexOptions.RightToLeft);
TextRange all = new TextRange(GetPoint(inp.Document.ContentStart, 0), GetPoint(inp.Document.ContentEnd, 0));
TextRange aa = new TextRange(GetPoint(inp.Document.ContentStart, 0), GetPoint(inp.CaretPosition, 0));
TextRange ab = new TextRange(GetPoint(inp.CaretPosition, 0), GetPoint(inp.Document.ContentEnd, 0));
Match b = srtl.Match(aa.Text);
Match c = sentence.Match(ab.Text);
TextRange cur = new TextRange(GetPoint(inp.CaretPosition, -b.Index), GetPoint(inp.CaretPosition.GetPositionAtOffset(c.Index + c.Length + b.Length), 0));
all.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Silver);
cur.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.White);