public bool FindNext(TextEditor editor, int minPos, int maxPos) { if (editor == null) { MessageBox.Show("No Text Editor is associated with this Find and Replace Dialog"); return false; } if (this.cboFind.Text == null || this.cboFind.Text.Equals(String.Empty)) { MessageBox.Show("No Find Text specified"); return false; } String find = this.cboFind.Text; //Validate Regex if (this.chkRegex.IsChecked == true) { try { Regex.IsMatch(String.Empty, find); } catch (Exception ex) { MessageBox.Show("Regular Expression is malformed - " + ex.Message); return false; } } //Add Search Text to Combo Box for later reuse if (!this.cboFind.Items.Contains(find)) { this.cboFind.Items.Add(find); } int start = editor.CaretOffset; int pos; int length = find.Length; StringComparison compareMode = (this.chkMatchCase.IsChecked == true) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; RegexOptions regexOps = (this.chkMatchCase.IsChecked == true) ? RegexOptions.None : RegexOptions.IgnoreCase; if (this.chkSearchUp.IsChecked == true) { //Search portion of Document prior to current position if (this.chkRegex.IsChecked == true) { MatchCollection ms = Regex.Matches(editor.Text.Substring(0, start), find, regexOps); if (ms.Count == 0) { pos = -1; } else { pos = ms[ms.Count - 1].Index; length = ms[ms.Count - 1].Length; } } else { pos = editor.Text.Substring(0, start).LastIndexOf(find, compareMode); } } else { //Search position of Document subsequent to current position (incl. any selected text) start += editor.SelectionLength; if (this.chkRegex.IsChecked == true) { Match m = Regex.Match(editor.Text.Substring(start), find, regexOps); if (!m.Success) { pos = -1; } else { pos = start + m.Index; length = m.Length; } } else { pos = editor.Text.IndexOf(find, start, compareMode); } } //If we've found the position of the next highlight it and return true otherwise return false if (pos > -1) { //Check we meet any document range restrictions if (pos < minPos || pos > maxPos) { editor.CaretOffset = pos; editor.SelectionStart = pos; editor.SelectionLength = length; return this.FindNext(editor, minPos, maxPos); } //If Matching on whole word ensure that their are boundaries before and after the match if (this.chkMatchWholeWord.IsChecked == true) { //Check boundary before if (pos > 0) { char c = editor.Text[pos - 1]; if (Char.IsLetterOrDigit(c)) { //Not a boundary so adjust start position and recurse editor.CaretOffset = pos + length; if (this.chkSearchUp.IsChecked == false) editor.CaretOffset -= editor.SelectionLength; return this.FindNext(editor); } } //Check boundary after if (pos + length < editor.Text.Length - 1) { char c = editor.Text[pos + length]; if (Char.IsLetterOrDigit(c)) { //Not a boundary so adjust start position and recurse editor.CaretOffset = pos + length - 1; if (this.chkSearchUp.IsChecked == false) editor.CaretOffset -= editor.SelectionLength; return this.FindNext(editor); } } } editor.Select(pos, length); editor.CaretOffset = pos; editor.ScrollTo(editor.Document.GetLineByOffset(pos).LineNumber, 0); return true; } else { return false; } }
public void ScrollTo(int line, int column) { _editor.ScrollTo(line, column); }