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

RichTextBox with Intellisense support

7 Answers 375 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
prabudda
Top achievements
Rank 1
prabudda asked on 23 Aug 2017, 10:51 AM

Hi

currently i'm trying to integrate Intellisense support to rad richtextbox. i was able to display word list when press Ctrl+Space. but could not replace the word. is there any one who try such a thing. i have attached a screen shot which was develop using visual studio richtextbox. can not use that code for RadRichTextBox.

7 Answers, 1 is accepted

Sort by
0
Anna
Telerik team
answered on 24 Aug 2017, 11:14 AM
Hi,

You must have already received the answer on your e-mail, but I am going to post it here as well.

What you can do is select the current word, delete it and replace it with the word chosen by the user. You would need something like the following:

using (DocumentPosition position = new DocumentPosition(richTextBox.Document.CaretPosition))
{
    position.MoveToPrevious();
 
    string currentWord = position.GetCurrentWord();
 
    if (currentWord == "hel")
    {
        SpanLayoutBox spanBox = position.GetCurrentSpanBox();
        if (spanBox != null)
        {
            Span currentSpan = spanBox.AssociatedSpan;
 
            int startIndex = currentSpan.Text.IndexOf(currentWord);
            int endIndex = startIndex + currentWord.Length;
 
            using (DocumentPosition start = new DocumentPosition(richTextBox.Document))
            {
                start.MoveToStartOfDocumentElement(currentSpan);
 
                for (int i = 0; i < startIndex; i++)
                {
                    start.MoveToNext();
                }
 
                using (DocumentPosition end = new DocumentPosition(start))
                {
                    for (int i = startIndex; i < endIndex; i++)
                    {
                        end.MoveToNext();
                    }
 
                    richTextBox.BeginUndoGroup();
 
                    richTextBox.Document.Selection.Clear();
                    richTextBox.Document.Selection.AddSelectionStart(start);
                    richTextBox.Document.Selection.AddSelectionEnd(end);
                    richTextBox.Delete(true);
 
                    richTextBox.Insert("hello");
                    richTextBox.EndUndoGroup("Intelisense");
                }
            }
        }
    }
}


What this code does is it gets the current caret position, the current word and the current span and from them it gets the start and the end position of the word. After that it can select the word using the two positions, delete it, and add the new word. The deletion and insertion are enclosed in an undo group, in order to appear as a single action in the history stack.

A few things to note: we move the caret position one step back, because if it is in the end of the word, the code would find the end of the document, instead of the word behind the position. You might want to extend the code to handle scenarios when the user has not yet started to type for example. Also, this algorithm would probably not perform well for cases when the word is split between spans, for example if the first half of it has a different format set, like bold.

I hope this helps.

Regards,
Anna
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
prabudda
Top achievements
Rank 1
answered on 25 Aug 2017, 01:41 PM

hi Anna

thank you for the response. i tried your solution, but it didnt work as i expect. so i came with my own solution with your recommendations.

private void AddTextToDocument(string insertText)
        {
           // Console.WriteLine(insertText);
            using (DocumentPosition positionInDocument = new DocumentPosition(this.Document.CaretPosition))
            {
 
                if (this.Document.CaretPosition.GetCurrentSpanBox().IsFormattingSymbol)
                {
                    positionInDocument.MoveToPrevious();
                }
                string word = positionInDocument.GetCurrentWord();
 
                using (DocumentPosition wordEndPosition = new DocumentPosition(positionInDocument))
                {
                    using (DocumentPosition wordStartPosition = new DocumentPosition(positionInDocument))
                    {
 
                        wordEndPosition.MoveToCurrentWordEnd();
                        if (!wordEndPosition.GetCurrentWord().Equals(" "))
                        {
                            wordStartPosition.MoveToCurrentWordStart();
                        }
 
                        
 
                        this.Document.Selection.AddSelectionStart(wordStartPosition);
                        this.Document.Selection.AddSelectionEnd(wordEndPosition);
                         
 
                        Console.WriteLine("positionInDocument.GetCurrentWord() " + positionInDocument.GetCurrentWord());
                        Console.WriteLine("wordStartPosition.GetCurrentWord() " + wordStartPosition.GetCurrentWord());
                        Console.WriteLine("wordEndPosition.GetCurrentWord() " + wordEndPosition.GetCurrentWord());
 
                        //this.ActiveDocumentEditor.Insert(insertText);
                        this.ActiveDocumentEditor.Insert(IntellisenseWordList.IntellisenseWordsDic[insertText]);
                    }
                }
            }
            this.Focus();
            AssistListBox.Visibility = Visibility.Collapsed;
}
0
Anna
Telerik team
answered on 28 Aug 2017, 08:27 AM
Hi Prabudda,

I am happy that you've reached a better solution and thank you for sharing it with us.

Please, let us know if anything else comes up.

Regards,
Anna
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Susmitha
Top achievements
Rank 1
Veteran
Iron
answered on 23 Apr 2020, 11:37 AM

Hi ,

Is there a feature like Autocomplete for text box in RichTextBox ?

I just want to add a specific set of key words to the starting line of the file ,some thing similar to Autocomplete feature in Radrich text box to do this.

Thanks in Advance

Susmitha

0
Dimitar
Telerik team
answered on 24 Apr 2020, 11:51 AM

Hello Susmitha,

This feature is not available in RadRichTextBox. The RadSyntaxEditor has a similar feature that you can use for this: IntelliPrompts.

Let me know how this works for you.

Regards,
Dimitar
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Susmitha
Top achievements
Rank 1
Veteran
Iron
answered on 29 Apr 2020, 06:56 AM

Hi Dimitar,

Thanks for the information.

But currently am using an old version of Telerik(RadSyntaxEditor is Beta in the R3 2019 release) ,so cant use this in my application.

Susmitha

0
Dimitar
Telerik team
answered on 29 Apr 2020, 09:40 AM

Hi Susmitha,

RadRichtextBox does not have built-in functionality for this. You can use the approach suggested above to implement it.

Let me know if you have any other questions.

Regards,
Dimitar
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
RichTextBox
Asked by
prabudda
Top achievements
Rank 1
Answers by
Anna
Telerik team
prabudda
Top achievements
Rank 1
Susmitha
Top achievements
Rank 1
Veteran
Iron
Dimitar
Telerik team
Share this question
or