Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        bool FindNext()
        {
            if (!bMatchDiacritics)
            {
                searchData = VietUtilities.StripDiacritics(textBox1.Text);
                strFind    = VietUtilities.StripDiacritics(strFind);
            }
            else
            {
                searchData = textBox1.Text;
            }

            if (bSearchDown)
            {
                int iStart = textBox1.SelectionStart + textBox1.SelectionLength;

                if (bMatchRegex || (bMatchWholeWord.HasValue && bMatchWholeWord.Value))
                {
                    try
                    {
                        if (bMatchWholeWord.HasValue && bMatchWholeWord.Value)
                        {
                            strFind = "\\b" + strFind + "\\b";
                        }
                        Regex regex = new Regex((bMatchCase ? string.Empty : "(?i)") + strFind, RegexOptions.Multiline);
                        Match m     = regex.Match(searchData, iStart);
                        if (m.Success)
                        {
                            textBox1.SelectionStart  = m.Index;
                            textBox1.SelectionLength = m.Length;
                            textBox1.Focus();
                            return(true);
                        }
                    }
                    catch (Exception e)
                    {
                        //logger.Error(e);
                        MessageBox.Show(e.Message, Properties.Resources.Regex_Error);
                        return(false);
                    }
                }
                else
                {
                    while (iStart + strFind.Length <= textBox1.Text.Length)
                    {
                        if (String.Compare(strFind, 0, searchData, iStart, strFind.Length, !bMatchCase) == 0)
                        {
                            textBox1.SelectionStart  = iStart;
                            textBox1.SelectionLength = strFind.Length;
                            textBox1.Focus();
                            return(true);
                        }
                        iStart++;
                    }
                }
            }
            else
            {
                if (bMatchRegex || (bMatchWholeWord.HasValue && bMatchWholeWord.Value))
                {
                    int iEnd = textBox1.SelectionStart;
                    try
                    {
                        //Regex regex = new Regex((bMatchCase ? string.Empty : "(?i)") + strFind, RegexOptions.Multiline);
                        //MatchCollection mc = regex.Matches(searchData.Substring(0, iEnd));
                        //if (mc.Count > 0)
                        //{
                        //    Match m = mc[mc.Count - 1]; // last match
                        //    textBox1.SelectionStart = m.Index;
                        //    textBox1.SelectionLength = m.Length;
                        //    return true;
                        //}
                        if (bMatchWholeWord.HasValue && bMatchWholeWord.Value)
                        {
                            strFind = "\\b" + strFind + "\\b";
                        }
                        Regex regex = new Regex((bMatchCase ? string.Empty : "(?i)") + string.Format("{0}(?!.*{0})", strFind), RegexOptions.Multiline | RegexOptions.Singleline);
                        Match m     = regex.Match(searchData, 0, iEnd);
                        if (m.Success)
                        {
                            textBox1.SelectionStart  = m.Index;
                            textBox1.SelectionLength = m.Length;
                            textBox1.Focus();
                            return(true);
                        }
                    }
                    catch (Exception e)
                    {
                        //logger.Error(e);
                        MessageBox.Show(e.Message, Properties.Resources.Regex_Error);
                        return(false);
                    }
                }
                else
                {
                    int iStart = textBox1.SelectionStart - strFind.Length;

                    while (iStart >= 0)
                    {
                        if (String.Compare(strFind, 0, searchData, iStart, strFind.Length, !bMatchCase) == 0)
                        {
                            textBox1.SelectionStart  = iStart;
                            textBox1.SelectionLength = strFind.Length;
                            textBox1.Focus();
                            return(true);
                        }
                        iStart--;
                    }
                }
            }

            MessageBoxResult result = MessageBox.Show(Properties.Resources.Cannot_find_ + "\"" + textFind + "\".\n" +
                                                      Properties.Resources.Continue_search_from_ + (bSearchDown ? Properties.Resources.beginning : Properties.Resources.end) + "?",
                                                      strProgName, MessageBoxButton.YesNo, MessageBoxImage.Exclamation);

            if (result == MessageBoxResult.Yes)
            {
                if (bSearchDown)
                {
                    textBox1.SelectionStart = 0;
                }
                else
                {
                    textBox1.SelectionStart = textBox1.Text.Length;
                }

                textBox1.SelectionLength = 0;
                FindNext();
            }
            return(false);
        }
Esempio n. 3
0
        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);
        }