none
WPF FAQ – Comment rechercher et remplacer du texte dans RichTextBox (2) RRS feed

  • Discussion générale

  • Comment rechercher et remplacer du texte dans RichTextBox (1)

     

     

     

        public Int32 ReplaceAll(String input, String replacement, FindOptions findOptions, Action<TextRange> action)
        {
           
    Int32 count = 0;
            currentPosition = inputDocument.ContentStart;
           
    while (currentPosition.CompareTo(inputDocument.ContentEnd) < 0)
            {
               
    TextRange textRange = Replace(input, replacement, findOptions);
               
    if (textRange != null)
                {
                    count++;
                   
    if (action != null)
                    {
                        action(textRange);
                    }
                }
            }

           
    return count;
        }

        public TextRange GetTextRangeFromPosition(ref TextPointer position,
                                                 
    String input,
                                                 
    FindOptions findOptions)
        {
           
    Boolean matchCase = (findOptions & FindOptions.MatchCase) == FindOptions.MatchCase;
           
    Boolean matchWholeWord = (findOptions & FindOptions.MatchWholeWord)
                                                        ==
    FindOptions.MatchWholeWord;
           
    TextRange textRange = null;
           
    while (position != null)
            {
               
    if (position.CompareTo(inputDocument.ContentEnd) == 0)
                {
                   
    break;
                }
               
    if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                   
    String textRun = position.GetTextInRun(LogicalDirection.Forward);
                   
    StringComparison stringComparison = matchCase ?
                       
    StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
                   
    Int32 indexInRun = textRun.IndexOf(input, stringComparison);
                   
    if (indexInRun >= 0)
                    {
                        position = position.GetPositionAtOffset(indexInRun);
                       
    TextPointer nextPointer = position.GetPositionAtOffset(input.Length);
                        textRange =
    new TextRange(position, nextPointer);
                       
    if (matchWholeWord)
                        {
                           
    if (IsWholeWord(textRange))
                            {
                                                   position = position.GetPositionAtOffset(input.Length);
                               
    break;
                            }
                           
    else
                            {
                      
                                position = position.GetPositionAtOffset(input.Length);
                               
    return GetTextRangeFromPosition(ref position, input, findOptions);
                            }
                        }
                       
    else
                        {
                                              position = position.GetPositionAtOffset(input.Length);
                           
    break;
                        }
                    }
                   
    else
                    {
                                          position = position.GetPositionAtOffset(textRun.Length);
                    }
                }
               
    else
                {
                                  position = position.GetNextContextPosition(
    LogicalDirection.Forward);
                }
            }

           
    return textRange;
        } 

        private Boolean IsWordChar(Char character)
        {
           
    return Char.IsLetterOrDigit(character) || character == '_';
        }

        private Boolean IsWholeWord(TextRange textRange)
        {
           
    Char[] chars = new Char[1];
           
    if (textRange.Start.CompareTo(inputDocument.ContentStart) == 0 || textRange.Start.IsAtLineStartPosition)
            {
                textRange.End.GetTextInRun(
    LogicalDirection.Forward, chars, 0, 1);
               
    return !IsWordChar(chars[0]);
            }
           
    else if (textRange.End.CompareTo(inputDocument.ContentEnd) == 0)
            {
                textRange.Start.GetTextInRun(
    LogicalDirection.Backward, chars, 0, 1);
               
    return !IsWordChar(chars[0]);
            }
           
    else
            {
                textRange.End.GetTextInRun(
    LogicalDirection.Forward, chars, 0, 1);
               
    if (!IsWordChar(chars[0]))
                {
                    textRange.Start.GetTextInRun(
    LogicalDirection.Backward, chars, 0, 1);
                   
    return !IsWordChar(chars[0]);
                }
            }
           
    return false;
        }
    }
    lundi 28 juin 2010 14:13