Programmatic find/replace?

12 posts, 0 answers
  1. Kjell
    Kjell avatar
    276 posts
    Member since:
    Jun 2010

    Posted 07 Dec 2010 Link to this post

    Hi, how would I go about doing doing a text find/replace programmatically?
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 09 Dec 2010 Link to this post

    Hello Kjell,

    There is a demo illustrating how you can use the Find functionality in this forum thread. With replacing, basically, you perform a search for a string and then, you can do as you please with the results. The method in the demo selects all occurrences of the word. By adding a line, you get ReplaceAll:

    private void buttonFind_Click(object sender, RoutedEventArgs e)
    {
        this.radRichTextBox.Document.Selection.Clear();
        DocumentTextSearch search = newDocumentTextSearch(this.radRichTextBox.Document);
        foreach (var textRange in search.FindAll(this.textBox.Text))
        { this.radRichTextBox.Document.Selection.AddSelectionStart(textRange.StartPosition);               this.radRichTextBox.Document.Selection.AddSelectionEnd(textRange.EndPosition);
    this.radRichTextBox.Insert(sometext);
        }         
    }

    Try it out and let us know if you experience any difficulties.

    Regards,
    Iva
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
  4. Adam
    Adam avatar
    32 posts
    Member since:
    Sep 2010

    Posted 07 Nov 2011 Link to this post

    I have a problem with this line:
    this.radRichTextBox.Insert(sometext)


    I am doing my search/replace entirely in code before I have a richTextBox. I'm actually doing it in a value converter. Is there a way to change the selected text without referencing a RadRichTextBox control?
  5. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 11 Nov 2011 Link to this post

    Hello Adam,

    In that case, you can use the following method:

    private void ReplaceAllMatches(RadDocument document, string toSearch, string toReplaceWith)
    {
        DocumentTextSearch search = new DocumentTextSearch(document);
        List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
        foreach (var textRange in search.FindAll(toSearch))
        {
            TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
            rangesTrackingDocumentChanges.Add(newRange);
        }
     
        foreach (var textRange in rangesTrackingDocumentChanges)
        {
            document.CaretPosition.MoveToPosition(textRange.StartPosition);
            document.DeleteRange(textRange.StartPosition, textRange.EndPosition);
     
            StyleDefinition style = new Span().ExtractStyleFromProperties();
            style.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
     
            document.Insert(toReplaceWith, style);
     
            textRange.StartPosition.Dispose();
            textRange.EndPosition.Dispose();
        }
    }

    Best wishes,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Adam
    Adam avatar
    32 posts
    Member since:
    Sep 2010

    Posted 22 Nov 2011 Link to this post

    Thank you for your response. One more small question regarding formatting:

    radDocument.CaretPosition.MoveToPosition(textRange.StartPosition);
    				radDocument.DeleteRange(textRange.StartPosition, textRange.EndPosition);
    				
    				StyleDefinition style = new Span().ExtractStyleFromProperties();
    				
    				radDocument.Insert(replace, style);
     
    				textRange.StartPosition.Dispose();
    				textRange.EndPosition.Dispose();


    This code results in the replaced text having a different font size than the original. I'm not sure I understand why - or what 
    new Span().ExtractStyleFromProperties() 
    
    does. How can I make sure the replaced text has exactly 
    the same style as the text it replaced?
  7. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 25 Nov 2011 Link to this post

    Hello Adam,

    The ExtractSTyleFromProperties() method creates a Style with the default style settings of a Span. In this way, it is certain that no property will remain having a null value, while at the same time you can set the properties you want to apply, such as the ForeColor property in my snippet.
    In order to use the current span style from the document, you have to extract it like this:

    SpanLayoutBox currentSpanBox = documentInCB.CaretPosition.GetCurrentSpanBox();
    Span currentSpan;
    if (currentSpanBox == null)
    {
        currentSpan = new Span();
    }
    else
    {
        currentSpan = currentSpanBox.AssociatedSpan;
    }
    StyleDefinition style = currentSpan.ExtractStyleFromProperties();

    Kind regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. Manuel
    Manuel avatar
    61 posts
    Member since:
    Oct 2011

    Posted 28 Dec 2011 Link to this post

    Hi Iva,

    I tried your code and got Object null Reference in FindAll() in bold. My code is here:

    private void ReplaceAllMatches(string toSearch, string toReplaceWith,RadDocument doc)
            {
                toSearch = Regex.Escape("min");
                //doc.Selection.Clear();
                
                DocumentTextSearch search = new DocumentTextSearch(doc);
                List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
                
               

                foreach (var textRange in search.FindAll(toSearch))
                {
                    TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
                    rangesTrackingDocumentChanges.Add(newRange);
                }

                foreach (var textRange in rangesTrackingDocumentChanges)
                {
                    doc.Selection.AddSelectionStart(textRange.StartPosition);
                    doc.Selection.AddSelectionEnd(textRange.EndPosition);
                    doc.Insert(toReplaceWith,null);
                    textRange.StartPosition.Dispose();
                    textRange.EndPosition.Dispose();
                }
            }

    I need some help here please,

    Thanks
  9. Manuel
    Manuel avatar
    61 posts
    Member since:
    Oct 2011

    Posted 29 Dec 2011 Link to this post

    Hi,

    I fix the problem!
    Thanks,
  10. kishore
    kishore avatar
    4 posts
    Member since:
    May 2014

    Posted 20 May 2014 in reply to Iva Toteva Link to this post

    Hi Iva,
    private void radButtonElementFindReplace_Click(object sender, EventArgs e)
            {
                using (FindAndReplace replaceBox = new FindAndReplace())
                {
                    if (replaceBox.ShowDialog() == DialogResult.OK)
                    {
                        string toSearch = (replaceBox.Controls.Find("textbox1", true).FirstOrDefault() as TextBox).Text;
                        string toReplaceWith = (replaceBox.Controls.Find("textbox2", true).FirstOrDefault() as TextBox).Text;
                        ReplaceAllMatches(this.radRichTextBox1.Document, toSearch, toReplaceWith);
                        replaceBox.Close();
                    }
     
                }
            }
     
            private void ReplaceAllMatches(RadDocument document, string toSearch, string toReplaceWith)
            {
                DocumentTextSearch search = new DocumentTextSearch(document);
                List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
                foreach (var textRange in search.FindAll(toSearch))
                {
                    TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
                    rangesTrackingDocumentChanges.Add(newRange);
                }
     
                foreach (var textRange in rangesTrackingDocumentChanges)
                {
                    document.CaretPosition.MoveToPosition(textRange.StartPosition);
                    document.DeleteRange(textRange.StartPosition, textRange.EndPosition);
     
                    StyleDefinition style = new Span().ExtractStyleFromProperties();
                    style.SetPropertyValue(Span.ForeColorProperty, Color.Red);
     
                    document.Insert(toReplaceWith, style);
     
                    textRange.StartPosition.Dispose();
                    textRange.EndPosition.Dispose();
                }
     
            }

    Can you pls explain how did u achieved this functionality. I had a Main Form on that Form I have a button which opens another Form with search and replace textboxes and a button to replace. But I am unable to hook up the replace button click event with this method. When I click the replace button, it's not doing anytying. I used the code below . if you can provide some info , it would be of great help

  11. Petya
    Admin
    Petya avatar
    975 posts

    Posted 23 May 2014 Link to this post

    Hi kishore,

    RadRichTextBox for Silverlight comes with a predefined Find/Replace dialog which you can use to replace all instances of a specific string in a document.  Here is how you can invoke the dialog in the click event handler of a button:
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.radRichTextBox.ShowFindReplaceDialog();
    }

    I hope this is helpful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  12. kishore
    kishore avatar
    4 posts
    Member since:
    May 2014

    Posted 23 May 2014 in reply to Petya Link to this post

    Hi Petya

    My bad I should have mentioned windows forms and haven't noticed that this thread is meant for sliverlight. I couldn't find the Find/ Replace dialog for windows forms. Please let me know.

    Thanks,
  13. Boby
    Admin
    Boby avatar
    595 posts

    Posted 26 May 2014 Link to this post

    Hello Kishore,
    Could you please open separate support thread in the Winforms forum on the matter, as this is the forum for RadRichTextBox for Silverlight specifically.

    Regards,
    Boby
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
DevCraft banner