This is a migrated thread and some comments may be shown as answers.

Format current sentence

0 Answers 19 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Ja
Top achievements
Rank 1
Ja asked on 17 Jul 2012, 11:49 AM

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); 

No answers yet. Maybe you can help?

Tags
RichTextBox
Asked by
Ja
Top achievements
Rank 1
Share this question
or