RichTextBox with Intellisense support

4 posts, 0 answers
  1. prabudda
    prabudda avatar
    2 posts
    Member since:
    Aug 2017

    Posted 23 Aug Link to this post

    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.

  2. Anna
    Admin
    Anna avatar
    109 posts

    Posted 24 Aug Link to this post

    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.
  3. prabudda
    prabudda avatar
    2 posts
    Member since:
    Aug 2017

    Posted 25 Aug Link to this post

    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;
    }
  4. Anna
    Admin
    Anna avatar
    109 posts

    Posted 28 Aug Link to this post

    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.
Back to Top