public async Task <TextRange> FindNextAsync(bool viaF3, bool searchBackward, string messageIfNotFound) { if (string.IsNullOrEmpty(txtLookFor.Text)) { MessageBox.Show(this, _noSearchString.Text, Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return(null); } _lastSearchWasBackward = searchBackward; _search.LookFor = txtLookFor.Text; _search.MatchCase = chkMatchCase.Checked; _search.MatchWholeWordOnly = chkMatchWholeWord.Checked; int startIdx = -1; int currentIdx = -1; TextRange range; do { Caret caret = _editor.ActiveTextAreaControl.Caret; if (viaF3 && _search.HasScanRegion && !Globals.IsInRange(caret.Offset, _search.BeginOffset, _search.EndOffset)) { // user moved outside of the originally selected region _search.ClearScanRegion(); UpdateTitleBar(); } int startFrom = caret.Offset - (searchBackward ? 1 : 0); if (startFrom == -1) { startFrom = _search.EndOffset; } range = _search.FindNext(startFrom, searchBackward, out _lastSearchLoopedAround); if (range != null && (!_lastSearchLoopedAround || _fileLoader == null)) { SelectResult(range); } else if (_fileLoader != null) { range = null; if (currentIdx != -1 && startIdx == -1) { startIdx = currentIdx; } if (_fileLoader(searchBackward, true, out var fileIndex, out var loadFileContent)) { currentIdx = fileIndex; try { await loadFileContent; } catch (OperationCanceledException) { break; } } else { break; } } }while (range == null && startIdx != currentIdx && currentIdx != -1); if (range == null && messageIfNotFound != null) { MessageBox.Show(this, messageIfNotFound, " ", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(range); }