void ReplaceDialogOnReplace(object obj, RoutedEventArgs ea) { FindReplaceDialog dlg = (FindReplaceDialog)obj; textFind = dlg.FindText; //itemsFind = dlg.FindCollection; // memorize find strings strFind = textFind; textReplace = dlg.ReplaceText; //itemsReplace = dlg.ReplaceCollection; // memorize replace strings strReplace = textReplace; bMatchCase = dlg.MatchCase; bMatchWholeWord = dlg.MatchWholeWord; bMatchRegex = dlg.MatchRegex; bMatchDiacritics = dlg.MatchDiacritics; bSearchDown = dlg.SearchDown; selectedText = textBox1.SelectedText; if (selectedText == string.Empty) { FindNext(); return; } if (!bMatchDiacritics) { strFind = VietUtilities.StripDiacritics(strFind); selectedText = VietUtilities.StripDiacritics(selectedText); } int start = textBox1.SelectionStart; if (bMatchRegex) { try { Regex regex = new Regex((bMatchCase ? string.Empty : "(?i)") + strFind, RegexOptions.Multiline); textBox1.SelectedText = regex.Replace(selectedText, Unescape(strReplace)); } catch (Exception e) { //logger.Error(e); MessageBox.Show(e.Message, Properties.Resources.Regex_Error); return; } } else if (String.Compare(strFind, selectedText, !bMatchCase) == 0) { textBox1.SelectedText = strReplace; } if (!bSearchDown) { textBox1.SelectionStart = start; } FindNext(); }
void FindDialogOnFindNext(object obj, RoutedEventArgs ea) { FindReplaceDialog dlg = (FindReplaceDialog)obj; textFind = dlg.FindText; //itemsFind = dlg.FindCollection; // memorize find strings strFind = textFind; bMatchCase = dlg.MatchCase; bMatchWholeWord = dlg.MatchWholeWord; bMatchRegex = dlg.MatchRegex; bMatchDiacritics = dlg.MatchDiacritics; bSearchDown = dlg.SearchDown; FindNext(); }
protected override void buttonFind_Click(object sender, RoutedEventArgs e) { if (OwnedWindows.Count > 0) { foreach (Window form in this.OwnedWindows) { FindReplaceDialog findDlg1 = form as FindReplaceDialog; if (findDlg1 != null) { findDlg1.Show(); return; } } } FindReplaceDialog frDlg = new FindReplaceDialog(); frDlg.Owner = this; //textBox1.HideSelection = false; frDlg.FindText = textFind; //if (itemsFind != null) // frDlg.FindCollection = itemsFind; // restore memorized find strings frDlg.ReplaceText = textReplace; //if (itemsReplace != null) // frDlg.ReplaceCollection = itemsReplace; // restore memorized replace strings frDlg.MatchCase = bMatchCase; frDlg.MatchWholeWord = bMatchWholeWord; frDlg.MatchDiacritics = bMatchDiacritics; frDlg.MatchRegex = bMatchRegex; frDlg.SearchDown = bSearchDown; frDlg.FindNext += new RoutedEventHandler(FindDialogOnFindNext); frDlg.Replace += new RoutedEventHandler(ReplaceDialogOnReplace); frDlg.ReplaceAll += new RoutedEventHandler(ReplaceDialogOnReplaceAll); frDlg.CloseDlg += new RoutedEventHandler(FindReplaceDialogOnCloseDlg); frDlg.Show(); }
void ReplaceDialogOnReplaceAll(object obj, RoutedEventArgs ea) { FindReplaceDialog dlg = (FindReplaceDialog)obj; string str = textBox1.Text; string strTemp; textFind = dlg.FindText; //itemsFind = dlg.FindCollection; // memorize find strings strFind = textFind; textReplace = dlg.ReplaceText; //itemsReplace = dlg.ReplaceCollection; // memorize replace strings strReplace = textReplace; bMatchDiacritics = dlg.MatchDiacritics; if (!bMatchDiacritics) { strFind = VietUtilities.StripDiacritics(strFind); strTemp = VietUtilities.StripDiacritics(str); } else { strTemp = str; } bMatchCase = dlg.MatchCase; bMatchRegex = dlg.MatchRegex; int count = 0; if (bMatchRegex || bMatchDiacritics) { // only for MatchDiacritics try { Regex regex = new Regex((bMatchCase ? string.Empty : "(?i)") + (bMatchRegex ? strFind : Regex.Escape(strFind)), RegexOptions.Multiline); MatchCollection mc = regex.Matches(str); count = mc.Count; str = regex.Replace(str, Unescape(strReplace)); } catch (Exception e) { //logger.Error(e); MessageBox.Show(e.Message, Properties.Resources.Regex_Error); return; } } //else if (bMatchCase && bMatchDiacritics) //{ // count = (str.Length - str.Replace(strFind, "").Length) / strFind.Length; // str = str.Replace(strFind, strReplace); //} else { StringBuilder strB = new StringBuilder(str); for (int i = 0; i <= strB.Length - strFind.Length;) { if (String.Compare(strTemp, i, strFind, 0, strFind.Length, !bMatchCase) == 0) { strB.Remove(i, strFind.Length); strB.Insert(i, strReplace); if (!bMatchDiacritics) { strTemp = VietUtilities.StripDiacritics(strB.ToString()); } else { strTemp = strB.ToString(); } i += strReplace.Length; count++; } else { i++; } } str = strB.ToString(); } if (str != textBox1.Text) { textBox1.Text = str; textBox1.SelectionStart = 0; textBox1.SelectionLength = 0; //textBox1.Modified = true; } MessageBox.Show(string.Format(Properties.Resources.ReplacedOccurrence, count), strProgName); }