//------------------------------------------------------------------------ private void mnuEditReplace_Click(object sender, EventArgs e) { if (ActiveSource != null) { frmFind finder = new frmFind('R'); finder.Show(this); } }
//======================================================================== public static bool FindNext() { if (Util.MainForm.ActiveSource == null) { return(false); } TextEditorControl ics = Util.MainForm.ActiveSource.icsEditor; if (prevFind.empty) { frmFind finder = new frmFind('F'); finder.Show(Util.MainForm); return(false); } //find next occurence of prevFind string toSearch = ics.Document.TextContent; bool wasFirst = (prevFind.index == -1); if (prevFind.restart) { prevFind.restart = false; prevFind.index = 0; } else if (wasFirst) { prevFind.index = 0; } else if (!prevFind.replacing) //don't change index if in replacing mode { if (prevFind.index == ics.ActiveTextAreaControl.Caret.Offset) { prevFind.index++; } else { prevFind.index = ics.ActiveTextAreaControl.Caret.Offset; } } int findLength = 0; if (!prevFind.regular) { StringComparison comparer = prevFind.sensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; prevFind.index = toSearch.IndexOf(prevFind.search, prevFind.index, comparer); findLength = prevFind.search.Length; } else { RegexOptions comparer = prevFind.sensitive ? RegexOptions.None : RegexOptions.IgnoreCase; try { prevFind.match = new Regex(prevFind.search, comparer | RegexOptions.Multiline).Match(toSearch, prevFind.index); if (prevFind.match.Success) { prevFind.index = prevFind.match.Index; findLength = prevFind.match.Value.Length; } else { prevFind.index = -1; } } catch (Exception ex) { MessageBox.Show("Error Creating Search (Bad RegEx String?)\nError: \"" + ex.Message + "\"", "PwrIDE - Find", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } //found anything? if (prevFind.index == -1) { if (wasFirst) { MessageBox.Show("\"" + prevFind.search + "\" not found.", "PwrIDE - Find", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Reached end of document.", "PwrIDE - Find", MessageBoxButtons.OK, MessageBoxIcon.Information); } ics.ActiveTextAreaControl.SelectionManager.ClearSelection(); prevFind.restart = true; return(false); } else { //get starting position int line = 0, idx, col, lasthit = 0; for (idx = 0; idx < prevFind.index; idx++) { if (toSearch[idx] == '\n') { line++; lasthit = idx + 1; } } col = prevFind.index - lasthit; TextLocation tlStart = new TextLocation(col, line); //get ending position int hit = -1; col += findLength; for (idx = 1; idx < findLength; idx++) { if (toSearch[idx + prevFind.index] == '\n') { hit = idx; } } if (hit > -1) { col -= hit; } TextLocation tlEnd = new TextLocation(col, line); //select found area ics.ActiveTextAreaControl.Caret.Position = tlStart; ics.ActiveTextAreaControl.SelectionManager.SetSelection(tlStart, tlEnd); //prevFind.index += prevFind.search.Length; return(true); } }