This is a migrated thread and some comments may be shown as answers.

Programmatic find/replace?

11 Answers 358 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Kjell
Top achievements
Rank 1
Kjell asked on 07 Dec 2010, 11:31 PM
Hi, how would I go about doing doing a text find/replace programmatically?

11 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 09 Dec 2010, 01:09 PM
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
0
Adam
Top achievements
Rank 1
answered on 07 Nov 2011, 09:46 PM
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?
0
Iva Toteva
Telerik team
answered on 11 Nov 2011, 12:18 PM
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 >>

0
Adam
Top achievements
Rank 1
answered on 22 Nov 2011, 04:52 PM
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?
0
Iva Toteva
Telerik team
answered on 25 Nov 2011, 03:58 PM
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 >>

0
Manuel
Top achievements
Rank 1
answered on 28 Dec 2011, 03:54 PM
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
0
Manuel
Top achievements
Rank 1
answered on 29 Dec 2011, 03:04 PM
Hi,

I fix the problem!
Thanks,
0
kishore
Top achievements
Rank 1
answered on 20 May 2014, 09:38 PM
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

0
Petya
Telerik team
answered on 23 May 2014, 12:03 PM
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.
 
0
kishore
Top achievements
Rank 1
answered on 23 May 2014, 01:22 PM
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,
0
Boby
Telerik team
answered on 26 May 2014, 07:27 AM
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.
 
Tags
RichTextBox
Asked by
Kjell
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Adam
Top achievements
Rank 1
Manuel
Top achievements
Rank 1
kishore
Top achievements
Rank 1
Petya
Telerik team
Boby
Telerik team
Share this question
or