11 Answers, 1 is accepted
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
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?
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();
}
}
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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?
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();
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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
I fix the problem!
Thanks,
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
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
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,
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