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

Programatically Highlight Text

14 Answers 602 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Steve Moss
Top achievements
Rank 1
Steve Moss asked on 23 Nov 2010, 09:28 PM
I need to be able to programatically highlight text.  I'm assuming that just grabbing the document and willy nilly setting highlights is a bad idea.
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

Sort by
0
Iva Toteva
Telerik team
answered on 24 Nov 2010, 02:36 PM
Hi Steve,

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.

Kind regards,
Iva
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Steve Moss
Top achievements
Rank 1
answered on 24 Nov 2010, 03:11 PM
Iva,

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
0
Ivailo Karamanolev
Telerik team
answered on 25 Nov 2010, 04:27 PM
Hello Steve Moss,

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
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Steve Moss
Top achievements
Rank 1
answered on 30 Nov 2010, 12:54 AM
Thanks.  There's no way I would have figured that one out.

0
jagmohan
Top achievements
Rank 2
answered on 11 Mar 2011, 02:21 PM
i want to highlight the text under the mouse position....like on mouse hover the text in the current line gets highlighted.....
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
0
Mike
Telerik team
answered on 17 Mar 2011, 10:49 AM
Hi Jagmohan,

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);
}

Regards,
Mike
the Telerik team
0
Dave
Top achievements
Rank 1
answered on 23 Mar 2011, 02:23 AM
Excuse my post against an old thread..

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? 



0
Boby
Telerik team
answered on 25 Mar 2011, 02:45 PM
Hello Steve Moss,

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
0
Dave
Top achievements
Rank 1
answered on 25 Mar 2011, 04:49 PM
Thanks for the reply.  I've included the System.Linq in the soultion and the method is accessible.

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();

 

 

}



0
Mike
Telerik team
answered on 28 Mar 2011, 04:48 PM
Hello Steve Moss,

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
0
Jason
Top achievements
Rank 1
answered on 27 Mar 2012, 09:46 PM
I am using the newest controls. I want to do something similar. I want to create functionality to drag an item from your tree control and drop it on to the editor in a certain location to associate it with a span. Actually, I want to do something a bit more complicated, but for simplicity let's say I want to do this by highlighting the span the mouse is over [while I am dragging]. I have experimented with this but it appears as i am having trouble. Without going through my issues, perhaps it is better if you provide a simple example.
0
Martin Ivanov
Telerik team
answered on 02 Apr 2012, 07:53 AM
Hi Steve,

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
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Prashant
Top achievements
Rank 1
answered on 02 Dec 2013, 08:06 PM
Hi,

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.
0
Petya
Telerik team
answered on 05 Dec 2013, 05:01 PM
Hi Prashant,

You could try creating your custom commands which will execute the wanted methods of RadRichTextBox.

I hope this helps!

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
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 >>
Tags
RichTextBox
Asked by
Steve Moss
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Steve Moss
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
jagmohan
Top achievements
Rank 2
Mike
Telerik team
Dave
Top achievements
Rank 1
Boby
Telerik team
Jason
Top achievements
Rank 1
Martin Ivanov
Telerik team
Prashant
Top achievements
Rank 1
Petya
Telerik team
Share this question
or