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

Highlighting a sentence

3 Answers 100 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Karl
Top achievements
Rank 1
Karl asked on 21 Feb 2012, 11:48 PM
Hi,

I've been playing around now for several hours with the question: What would be the best way to highlight a sentence? Keep in mind some sentences end with  "?" or "..."  By clicking on a button I would like the next sentence to be highlighted. Has anyone worked on this? 

Here's a code I have been working with. However, it only highlights the last word of a sentence, but not the last entire sentence.
    private void radButtonElement1_Click(object sender, EventArgs e)
    {
        this.radRichTextBox1.ChangeTextHighlightColor(System.Drawing.Color.White);
        this.radRichTextBox1.Document.Selection.Clear();
        this.radRichTextBox1.Document.Selection.AddSelectionStart(myNewSentenceStartPos);
        myEndPos.MoveToCurrentWordEnd();
        do
        {
            string word = myStartPos.GetCurrentSpanBox().Text;
            if (word.Contains("."))
            {
                DocumentPosition wordEndPosition = new DocumentPosition(myStartPos);
                wordEndPosition.MoveToCurrentWordEnd();
                this.radRichTextBox1.Document.Selection.Clear();
                this.radRichTextBox1.Document.Selection.AddSelectionStart(myNewSentenceStartPos);
                this.radRichTextBox1.Document.Selection.AddSelectionEnd(wordEndPosition);
                this.radRichTextBox1.ChangeTextHighlightColor(System.Drawing.Color.Aqua);  
                myStartPos = myEndPos;
                myNewSentenceStartPos = myEndPos;
                break;
            }
            else { }
        }
        while (myStartPos.MoveToNextWordStart());  
}

How can I set the start of the selection to myNewSentenceStartPos ?


Thank you,
Karl

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 24 Feb 2012, 04:10 PM
Hello Karl,

Thank you for writing.

Here is a very simple example, demonstrating how to highlight the next sentance in a text. Please note that this example takes the current caret position and moves forward through the words, while it finds the first word starting with capital letter (or no more words exist) and then it saves this as a starting position for the selection. Then it continues looping through the words while finding the next word starting with a capital letter (or no more words exist) and saves the end position for the selection. Then the text is being highlighted and the selection cleared:
public Form1()
{
    InitializeComponent();
 
    radRichTextBox1.Document.Insert(
        "Lorem ipsum is simply dummy text of the printing and typesetting industry. Lorem ipsum has been the industry's standard dummy text " +
        "ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not " +
        "only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with " +
        "the release of letraset sheets containing lorem ipsum passages, and more recently with desktop publishing software like aldus pageMaker " +
        "including versions of lorem ipsum.",
        new Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition());
}
 
private void radButton1_Click(object sender, EventArgs e)
{
    DocumentPosition startPosition = new DocumentPosition(this.radRichTextBox1.Document.CaretPosition);
    string word = startPosition.GetCurrentSpanBox().Text;
 
    while (!char.IsUpper(word[0]) && startPosition.MoveToNextWordStart())
    {
         
        word = startPosition.GetCurrentSpanBox().Text;
    }
 
    DocumentPosition wordEndPosition = new DocumentPosition(startPosition);
    word = "someLowerLetter";
 
    while (!char.IsUpper(word[0]) && wordEndPosition.MoveToNextWordStart())
    {
        word = wordEndPosition.GetCurrentSpanBox().Text;
    }
 
    this.radRichTextBox1.Document.Selection.AddSelectionStart(startPosition);
    this.radRichTextBox1.Document.Selection.AddSelectionEnd(wordEndPosition);
    this.radRichTextBox1.ChangeTextHighlightColor(System.Drawing.Color.Red);
    this.radRichTextBox1.Document.Selection.Clear();
}

Please note that this example demonstrates how to highlight text and how to loop through the text, however it cannot be considered for a complete solution. Feel free to extend it according to your needs - for example to look for ".", "...", "?", "!", or whatever you find convenient for your application.

I hope that you find this information useful.

Greetings,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Karl
Top achievements
Rank 1
answered on 24 Feb 2012, 09:54 PM
Hi Stephan.

Thank you for your example. I would like to share with our community how I have solved the sentence hightlight issue with your help:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
 
using Telerik.WinControls.RichTextBox;
using Telerik.WinControls.RichTextBox.Model;
 
namespace PT01
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public DocumentPosition myStartPos;
        public DocumentPosition myEndPos;
        public DocumentPosition myNewSentenceStartPos;
 
        public RadForm1()
        {
            InitializeComponent();
        }
 
        private void RadForm1_Load(object sender, EventArgs e)
        {
            radRichTextBox1.Document.Insert(
                "ALorem ipsum is simply dummy text of the printing and typesetting industry. BLorem ipsum has been the industry's standard dummy text " +
                "ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book! It has survived not " +
                "only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged? It was popularised in the 1960s with " +
                "the release of letraset sheets containing lorem ipsum passages, and more recently with desktop publishing software like aldus pageMaker " +
                "including versions of lorem ipsum.",
                new Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition());
            this.radRichTextBox1.Document.LayoutMode = DocumentLayoutMode.Paged;
            this.radRichTextBox1.Focus();
            this.ribbonTab1.IsSelected = true;
            movetop();
            myNewSentenceStartPos = myEndPos;
        }
 
        private void radButtonElement1_Click(object sender, EventArgs e)
        {
            deletePreviousHighlight();
            DocumentPosition startPosition = new DocumentPosition(myEndPos);
            myNewSentenceStartPos = startPosition;
 
            string word = startPosition.GetCurrentSpanBox().Text;
            while (!char.IsUpper(word[0]) && startPosition.MoveToNextWordStart())
            {
                word = startPosition.GetCurrentSpanBox().Text;
            }
 
            DocumentPosition wordEndPosition = new DocumentPosition(startPosition);
            word = "someLowerLetter";
            do
            {
                word = wordEndPosition.GetCurrentSpanBox().Text;
                if (myendofSentences(word))
                    break;
            }
            while (wordEndPosition.MoveToNextWordStart());
            wordEndPosition.MoveToNextWordStart();
            myEndPos = wordEndPosition;
            this.radRichTextBox1.Document.Selection.AddSelectionStart(startPosition);
            this.radRichTextBox1.Document.Selection.AddSelectionEnd(wordEndPosition);
            this.radRichTextBox1.ChangeTextHighlightColor(System.Drawing.Color.Red);
            this.radRichTextBox1.Document.Selection.Clear();
 
        }
        private bool myendofSentences(string myword)
        {
            bool myreturn = false;
            char[] chars = { '.', '!', '?', '"' };
            foreach (char ch in chars)
            {
                if (myword.Contains(ch.ToString()))
                    myreturn = true;
            }
            return myreturn;
        }
 
        private void deletePreviousHighlight()
        {
            DocumentPosition lastSentenceStartPosition = new DocumentPosition(myNewSentenceStartPos);
            DocumentPosition lastSentenceEndPosition = new DocumentPosition(myEndPos);
            this.radRichTextBox1.Document.Selection.AddSelectionStart(lastSentenceStartPosition);
            this.radRichTextBox1.Document.Selection.AddSelectionEnd(lastSentenceEndPosition);
            this.radRichTextBox1.ChangeTextHighlightColor(System.Drawing.Color.White);
            this.radRichTextBox1.Document.Selection.Clear();
        }
 
        private void radButtonElement2_Click(object sender, EventArgs e)
        {
            movetop();
        }
 
        private void movetop()
        {
            DocumentPosition startPosition = new DocumentPosition(this.radRichTextBox1.Document.CaretPosition);
            DocumentPosition endPosition = new DocumentPosition(startPosition);
            startPosition.MoveToFirstPositionInDocument();
            DocumentPosition myNewSentenceStartPos = new DocumentPosition(startPosition);
            startPosition.MoveToCurrentWordStart();
            myStartPos = startPosition;
            myEndPos = endPosition;
        }
    }
}

Sometimes solutions others have worked on help.

Thanks,
Karl
0
Stefan
Telerik team
answered on 29 Feb 2012, 01:01 PM
Thank you for sharing your solution with the community. I am sure that someone would benefit from it.

Regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Karl
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Karl
Top achievements
Rank 1
Share this question
or