Esempio n. 1
0
        public bool ReplaceAndSelect(TextPointer start, bool round, out TextPointer endPosition, string findText,
                                     string replaceText)
        {
            endPosition = null;

            if (String.IsNullOrEmpty(findText) || start == null)
            {
                return(true);
            }

            if (_manager == null)
            {
                _manager = new FindAndReplaceManager(FlowDocument);
            }

            _manager.CurrentPosition = start;

            TextRange textRange = _manager.Replace(findText, replaceText, GetFindOptions());

            if (textRange != null)
            {
                endPosition = textRange.End;
                return(SearchString(endPosition, true, true));
            }
            if (_manager.CurrentPosition.CompareTo(FlowDocument.ContentEnd) == 0 && round)
            {
                return(ReplaceAndSelect(FlowDocument.ContentStart, false, out endPosition, findText, replaceText));
            }
            RaiseMatchingChangedEvent(true, null, true);
            return(false);
        }
Esempio n. 2
0
        private void bFindReplace_Click(object sender, RoutedEventArgs e)
        {
            var         content     = frameSidebar.Content as Page;
            var         grid        = content.Content as Grid;
            TextBox     tbFind      = (TextBox)grid.Children[2];
            TextBox     tbReplace   = (TextBox)grid.Children[4];
            var         expander    = (Expander)grid.Children[9];
            CheckBox    cbMatchCase = (CheckBox)((Grid)expander.Content).Children[0];
            CheckBox    cbMatchWord = (CheckBox)((Grid)expander.Content).Children[1];
            TextRange   textRange   = null;
            FindOptions findOptions = FindOptions.None;

            if (cbMatchCase.IsChecked == true)
            {
                findOptions = FindOptions.MatchCase;
            }
            if (cbMatchWord.IsChecked == true)
            {
                findOptions ^= FindOptions.MatchWholeWord;
            }
            if (findReplaceChanges)
            {
                findAndReplace     = new FindAndReplaceManager(rtbMain.Document);
                findReplaceChanges = false;
            }
            switch (((Button)sender).Content.ToString())
            {
            case "Find Next":
                textRange = findAndReplace.FindNext(tbFind.Text, findOptions);
                break;

            case "Replace All":
                int results = findAndReplace.ReplaceAll(tbFind.Text, tbReplace.Text, findOptions, null);
                customMessageBox customMessageBox = new customMessageBox();
                customMessageBox.SetupMsgBox($"Replaced {results} occurences.", "Replace All", this.FindResource("iconInformation"));
                customMessageBox.ShowDialog();
                rtbMain.Focus();
                return;

            case "Replace Next":
                textRange = findAndReplace.Replace(tbFind.Text, tbReplace.Text, findOptions);
                break;

            case "Count":
                RichTextBox rtbTemp = XamlReader.Parse(XamlWriter.Save(rtbMain)) as RichTextBox;
                findAndReplace = new FindAndReplaceManager(rtbTemp.Document);
                int count = findAndReplace.ReplaceAll(tbFind.Text, "", findOptions, null);
                customMessageBox customMessageBox2 = new customMessageBox();
                customMessageBox2.SetupMsgBox($"\"{tbFind.Text}\" appeared {count} times.", "Count", this.FindResource("iconInformation"));
                customMessageBox2.ShowDialog();
                findReplaceChanges = true;
                return;

            default:
                break;
            }

            if (textRange == null)
            {
                customMessageBox customMessageBox = new customMessageBox();
                customMessageBox.SetupMsgBox("No (more) results.\nDo you wish to start at the top again?", "Reset search?", this.FindResource("iconInformation"), "Yes", "No");
                customMessageBox.ShowDialog();
                if (customMessageBox.result.ToString() == "Yes")
                {
                    findAndReplace     = new FindAndReplaceManager(rtbMain.Document);
                    findReplaceChanges = false;
                    textRange          = findAndReplace.FindNext(tbFind.Text, findOptions);
                    if (textRange == null)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            rtbMain.Selection.Select(textRange.Start, textRange.End);
            rtbMain.Focus();
        }