private void SelectText(string p) { TextRange tr = new TextRange(this.rtb.Document.ContentStart, this.rtb.Document.ContentEnd); int startPosition = tr.Text.IndexOf(p); if (startPosition > -1) { int lines = this.GetLinesCount(tr.Text, startPosition); TextPointer startRtb = this.rtb.Document.ContentStart; TextPointer start = startRtb.GetPositionAtOffset(startPosition + lines); TextPointer end = start.GetPositionAtOffset(p.Length); this.rtb.Selection.Select(start, end); this.rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, this.rtb.SelectionBrush); } } private int GetLinesCount(string str, int startPosition) { int lines = str // get Text up to startPosition .Take(startPosition) // Count "\n" and "\r" .Count(k => k == Environment.NewLine[0] || k == Environment.NewLine[1]); // lines + 1 because if not, missing lines with searched tring return lines + 1; }
this.rtb.Selection.Select(start, end); this.rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, this.rtb.SelectionBrush);
private void SelectText(string p) { TextRange tr = new TextRange(this.rtb.Document.ContentStart, this.rtb.Document.ContentEnd); int startPosition = tr.Text.IndexOf(p); int lines tr.Text.Count(k> k == Environment.NewLine[0] || k == Environment.NewLine[1]); TextPointer startRtb = this.rtb.Document.ContentStart; TextPointer start = startRtb.GetPositionAtOffset(startPosition + lines); TextPointer end = start.GetPositionAtOffset(p.Length); this.rtb.Selection.Select(start, end); this.rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, this.rtb.SelectionBrush); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprivate void SelectText() { RechercheRemplacement rR = new RechercheRemplacement(); //fenêtre pour la recherche rR.Title = "Recherche simple"; if (rR.ShowDialog().Value == true) { string wordToSearch = rR.rechercheTextBox.Text; TextRange tr = new TextRange(this.rtb.Document.ContentStart, this.rtb.Document.ContentEnd); int startPosition = tr.Text.IndexOf(wordToSearch); int lines = this.GetLinesCount(tr.Text, startPosition); TextPointer startRtb = this.rtb.Document.ContentStart; TextPointer start = startRtb.GetPositionAtOffset(startPosition + lines); TextPointer end = start.GetPositionAtOffset(wordToSearch.Length); this.rtb.Selection.Select(start, end); this.rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, this.rtb.SelectionBrush); this.rtb.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); this.rtb.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic); this.rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Yellow)); } } public int GetLinesCount(string str, int startPosition) { // Count "\n" and "\r" int lines str.Take(startPosition).Count(k> k.Equals((char)13) || k.Equals((char)10)); //lines + 2 because if not, missing lines with //searched string (2 because two chars to jump a line) return lines + 2; }
public void highLight(Color c) { Regex reg = new Regex(word); TextPointer startRtb = rtb.Document.ContentStart; TextPointer start; TextPointer end; while ((startRtb != null) && startRtb.CompareTo(rtb.Document.ContentEnd) < 0) { if (startRtb.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { Match match = reg.Match(startRtb.GetTextInRun(LogicalDirection. Forward)); //récupère la range avec l'index du mot et sa longueur start = startRtb.GetPositionAtOffset(match.Index, LogicalDirection.Forward); end = startRtb.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward); //highlight du texte tr = new TextRange(start, end); tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(c)); //on rebascule à fin de la range pour parcourir la suite startRtb = tr.End; } startRtb = startRtb.GetNextContextPosition(LogicalDirection.Forward); } }