Programatically Highlight Text

15 posts, 0 answers
  1. Steve Moss
    Steve Moss avatar
    6 posts
    Member since:
    Oct 2009

    Posted 23 Nov 2010 Link to this post

    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
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 24 Nov 2010 Link to this post

    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
  3. DevCraft banner
  4. Steve Moss
    Steve Moss avatar
    6 posts
    Member since:
    Oct 2009

    Posted 24 Nov 2010 Link to this post

    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
  5. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 25 Nov 2010 Link to this post

    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
  6. Steve Moss
    Steve Moss avatar
    6 posts
    Member since:
    Oct 2009

    Posted 29 Nov 2010 Link to this post

    Thanks.  There's no way I would have figured that one out.

  7. jagmohan
    jagmohan avatar
    4 posts
    Member since:
    Mar 2011

    Posted 11 Mar 2011 Link to this post

    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
  8. Mike
    Admin
    Mike avatar
    640 posts

    Posted 17 Mar 2011 Link to this post

    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
  9. Dave
    Dave avatar
    32 posts
    Member since:
    Feb 2008

    Posted 22 Mar 2011 Link to this post

    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? 



  10. Boby
    Admin
    Boby avatar
    595 posts

    Posted 25 Mar 2011 Link to this post

    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
  11. Dave
    Dave avatar
    32 posts
    Member since:
    Feb 2008

    Posted 25 Mar 2011 Link to this post

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

     

     

    }



  12. Mike
    Admin
    Mike avatar
    640 posts

    Posted 28 Mar 2011 Link to this post

    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
  13. Jason
    Jason avatar
    9 posts
    Member since:
    Jul 2008

    Posted 27 Mar 2012 Link to this post

    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.
  14. Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 02 Apr 2012 Link to this post

    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 >>
  15. Prashant
    Prashant avatar
    2 posts
    Member since:
    Nov 2013

    Posted 02 Dec 2013 Link to this post

    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.
  16. Petya
    Admin
    Petya avatar
    975 posts

    Posted 05 Dec 2013 Link to this post

    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 >>
Back to Top
DevCraft banner