Open KaustubhPatange opened 5 years ago
I would like to share what I did:
public virtual void FindPrev(string pattern)
{
try
{
RegexOptions opt = cbMatchCase.Checked ? RegexOptions.None : RegexOptions.IgnoreCase;
if (!cbRegex.Checked)
pattern = Regex.Escape(pattern);
if (cbWholeWord.Checked)
pattern = "\\b" + pattern + "\\b";
Range selectedRange = tb.Selection.Clone();
selectedRange.Normalize();
Range searchRange = new Range(tb);
// Search range before selection
searchRange.Start = new Place(0, 0);
searchRange.End = selectedRange.Start;
foreach (var r in searchRange.GetRangesByLinesReversed(pattern, opt))
{
tb.Selection = r;
tb.DoSelectionVisible();
tb.Invalidate();
return;
}
// Search range after selection
searchRange.Start = selectedRange.End;
searchRange.End = new Place(tb.GetLineLength(tb.LinesCount - 1), tb.LinesCount - 1);
foreach (var r in searchRange.GetRangesByLinesReversed(pattern, opt))
{
tb.Selection = r;
tb.DoSelectionVisible();
tb.Invalidate();
return;
}
MessageBox.Show("Not found");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I also changed the method FindNext
in the same was to make the search start again when the end of the search was reached.
For me the original code was very hard to understand and impossible to adapt for the backward search.
Hi, In your Find form there is a function "FindNext" which searches for next words (also given below). How to modify it for previous search like Find Previous word, something like that.