I will need to find all instances of a word in a document and apply a highlight to the word. Think of automatically highlighting keywords or search words. Also to clear all highlights of a specific color from a document.
Any suggestions or direction would be greatly appreciated.
Thanks,
Steve
14 Answers, 1 is accepted
As you have guessed, highlighting text as you type will not be a very good idea. The document will need to be scanned at small intervals of time and there will be performance issues.
On the other hand, if you load a document and want to find and highlight keywords on your command, you can easily do that. I have prepared a simple demo illustrating how you can programmatically find keywords and highlight them. Note that currently text-search is case-sensitive only, however we will add options about this in the next minor update. I have also included altering the highlighting of words, which have their highlight color set to some predefined value. If you set the new highlight color to Transparent, that will be equivalent to removing the highlights.
Take a look at the demo and let us know if you need further assistance.
Iva
the Telerik team
Thank you so much. As described this should be perfect for highlighting.
I would like to capture the current selection so that once the highlighting is complete I can return the user to where she's working. I'll poke around and see what looks possible. Any suggestions would be great.
Thanks,
Steve
Here's the entire listing of a method which saves the selection, searches for some text in the document, highlights it and restores the selection. It looks very similar to the demo in the other thread excluding the added code to save and restore the selection.
Tuple<DocumentPosition, DocumentPosition>[] positions =
this
.radRichTextBox.Document.Selection.Ranges
.Select(r =>
new
Tuple<DocumentPosition, DocumentPosition>(r.StartPosition, r.EndPosition))
.ToArray();
foreach
(var pos
in
positions)
{
pos.Item1.AnchorToNextFormattingSymbol();
pos.Item2.AnchorToNextFormattingSymbol();
}
this
.radRichTextBox.Document.Selection.Clear();
DocumentTextSearch search =
new
DocumentTextSearch(
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.ChangeTextHighlightColor(Colors.Yellow);
this
.radRichTextBox.Document.Selection.Clear();
foreach
(var pos
in
positions)
{
pos.Item1.RemoveAnchorFromNextFormattingSymbol();
pos.Item2.RemoveAnchorFromNextFormattingSymbol();
this
.radRichTextBox.Document.Selection.AddSelectionStart(pos.Item1);
this
.radRichTextBox.Document.Selection.AddSelectionEnd(pos.Item2);
}
Kind regards,
Ivailo
the Telerik team
right now m using getcurrentspanbox() for highlighting but it highlights only that text where the cursor is located.but i want the same thing on mouse move as the mouse position changes the cursor selection should also change
You can use the following code to move Document's caret to mouse position on mouse hover:
this
.editor.MouseMove += editor_MouseMove;
...
void
editor_MouseMove(
object
sender, System.Windows.Input.MouseEventArgs e)
{
var position =
this
.editor.ActiveEditorPresenter.GetDocumentPositionFromViewPoint(e.GetPosition(
this
.editor));
this
.editor.Document.CaretPosition.MoveToPosition(position);
}
Mike
the Telerik team
I'm using RadControls for Silverlight, v.2011.1.315.1040 RadTextBox and have attempted to use:
this.radRichTextBox.Document.Selection.Ranges
.Select(r =>
new Tuple<DocumentPosition, DocumentPosition>(r.StartPosition, r.EndPosition))
.ToArray();
the .Select is inaccessible?
Select is actually an extension method of IEnumerable. To use it, you must add the following using statement:
using
System.Linq;
Don't hesitate to contact us if you have other questions.
Best wishes,Boby
the Telerik team
Now, I've attempted to clear the highlight and experienced this exception
Unable to cast object of type 'Telerik.Windows.Documents.Model.HyperlinkRangeStart' to type 'Telerik.Windows.Documents.Model.Span'.
I'm using version v.2011.1.315.1040
private void buttonClearHighlight_Click(object sender, RoutedEventArgs e)
{
foreach (Block block in this.radRichTextBox.Document.Sections.First.Blocks)
{
foreach (Span span in block.Children)
{
if (span.HighlightColor == Colors.LightGray)
{
span.HighlightColor =
Colors.Transparent;
}
}
}
this.radRichTextBox.UpdateEditorLayout();
}
You should modify the code the following way:
foreach
(var inline
in
block.Children)
{
var span = inline
as
Span;
if
(span !=
null
&& span.HighlightColor == Colors.LightGray)
{
}
...
}
Kind regards,
Mike
the Telerik team
Here is an article about how Telerik RadDragAndDropManager.
After you handle the drag and drop you can get the item under the mouse with the
And similar to your scenario : drop bteween ListBox and TreeView
If the problems persist you can send us the sample you have trouble with in a support ticket.
All the best,
Martin
the Telerik team
I have been trying to highlight some text in a RadRichTextBox . Now I am using the MVVM pattern so I cannot directly refer to the RadRichTextBox in my code, I tried data binding to the document property of the control but that is not allowed.
Although I am able to find the text I need to highlight on my RadDocument in my viewmodel and I am able to apply the highlight as well how should I get the RadRichTextBox to show the highlighted text? Since there is no way for me to attach it to the RadCocument in my view model?
If I use a dataprovider then I am binding to the string itself and have no way to highlight anything and if I use a rad Document I cannot bind it to my RadRichTextBox....
Please help...
Regards,
Prashant.
You could try creating your custom commands which will execute the wanted methods of RadRichTextBox.
I hope this helps!
Regards,
Petya
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>