Winforms RadRichTextBox with Spellcheck Context Menu

Thread is closed for posting
6 posts, 0 answers
  1. 24667C81-31F8-4485-B7D7-FD5EB21D09B5
    24667C81-31F8-4485-B7D7-FD5EB21D09B5 avatar
    2 posts
    Member since:
    Jan 2012

    Posted 21 Mar 2013 Link to this post

    Requirements

    RadControls version

     winforms 2013.1.220.40

    .NET version

     4.0

    Visual Studio version

     2010

    programming language

     C#

    browser supp

    n/a


    PROJECT DESCRIPTION
    Use this RadRichTextBox subclass to add a context menu with spellcheck and normal (cut, copy, paste, etc) operations.

    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 System.Collections;
    using Telerik.WinControls.RichTextBox;
    using Telerik.WinControls.RichTextBox.Layout;
    using Telerik.WinControls.UI;
    using Telerik.WinControls.RichTextBox.Model;
    using System.Diagnostics;
    using System.Linq;
    using Telerik.WinControls.RichTextBox.Proofing;
      
      
        class MyRadRichTextBox : RadRichTextBox
        {
      
      
            private void ReplaceCurrentWord(SpanLayoutBox spanBox, string newWord)
             
                this.Document.CaretPosition.MoveToInline(spanBox, 0);
      
                DocumentPosition endPosition = new DocumentPosition(this.Document.CaretPosition);
                MoveToCurrentWordEnd(endPosition);
      
                this.Document.Selection.Clear();
                this.Document.Selection.AddSelectionStart(this.Document.CaretPosition);
                this.Document.Selection.AddSelectionEnd(endPosition);
      
                this.Insert(newWord);
            }
      
      
            /// <summary>
            /// DocumentPosition.MoveToCurrentWorkEnd was including characters other than Alpha-numerics.
            /// This function will stop at non alphanumeric characters.
            /// </summary>
            /// <param name="position"></param>
            private void MoveToCurrentWordEnd(DocumentPosition position)
            {
                position.MoveToCurrentWordEnd();
      
                this.Document.Selection.Clear();
                this.Document.Selection.AddSelectionStart(this.Document.CaretPosition);
                this.Document.Selection.AddSelectionEnd(position);
      
                string selectedText = this.Document.Selection.GetSelectedText();
      
                int length = selectedText.Length;
                int indexOfEnd = selectedText.TakeWhile(c => char.IsLetterOrDigit(c)).Count();
      
                while (indexOfEnd < length)
                {
                    position.MoveToPrevious();
                    length--;
                }
      
                this.Document.Selection.Clear();
            }
      
      
            private RadDropDownMenu BuildContextMenu(SpanLayoutBox spanBox)
            {
                RadDropDownMenu menu = new RadDropDownMenu();
                RadMenuItem menuItem;
      
      
                if (this.Document.Selection.IsEmpty && this.IsSpellCheckingEnabled && spanBox != null)
                {
      
                    this.Document.CaretPosition.MoveToInline(spanBox, 0);
      
                    string spanBoxTextAlphaNumericOnly = String.Concat(spanBox.Text.TakeWhile(c => char.IsLetterOrDigit(c)));
      
                    if (spanBoxTextAlphaNumericOnly.Length > 0 && !this.SpellChecker.CheckWordIsCorrect(spanBoxTextAlphaNumericOnly))
                    {
      
                        string[] suggestions = (string[])this.SpellChecker.GetSuggestions(spanBox.Text);
      
                        if (suggestions.Length <= 0)
                        {
                            menuItem = new RadMenuItem("(No Spelling Suggestions)");
                            menuItem.Enabled = false;
                            menu.Items.Add(menuItem);
                        }
      
                        foreach (string suggestion in suggestions)
                        {
                            menuItem = new RadMenuItem(suggestion);
                            menuItem.Click += (object sender, EventArgs e) => { ReplaceCurrentWord(spanBox, (sender as RadMenuItem).Text); };
                            menu.Items.Add(menuItem);
                        }
      
                        menu.Items.Add(new SeparatorElement());
      
                        menuItem = new RadMenuItem("Add to Dictionary");
                        menuItem.Click += (object sender, EventArgs e) => { this.SpellChecker.AddWord(spanBoxTextAlphaNumericOnly); };
                        menu.Items.Add(menuItem);
      
                        menu.Items.Add(new SeparatorElement());
                    }
      
                }
      
      
                menuItem = new RadMenuItem("Undo");
                menuItem.Click += (object sender, EventArgs e) => { this.Undo(); };
                menu.Items.Add(menuItem);
      
                menu.Items.Add(new SeparatorElement());
      
                menuItem = new RadMenuItem("Cut");
                menuItem.Click += (object sender, EventArgs e) => { this.Cut(); };
                menu.Items.Add(menuItem);
      
                menuItem = new RadMenuItem("Copy");
                menuItem.Click += (object sender, EventArgs e) => { this.Copy(); };
                menu.Items.Add(menuItem);
      
                menuItem = new RadMenuItem("Paste");
                menuItem.Click += (object sender, EventArgs e) => { this.Paste(); };
                menu.Items.Add(menuItem);
      
                menuItem = new RadMenuItem("Delete");
                menuItem.Click += (object sender, EventArgs e) => { this.Delete(false); };
                menu.Items.Add(menuItem);
      
                menu.Items.Add(new SeparatorElement());
      
                menuItem = new RadMenuItem("Select All");
                menuItem.Click += (object sender, EventArgs e) => { this.Document.Selection.SelectAll(); };
                menu.Items.Add(menuItem);
      
                return menu;
      
            }
      
      
      
            protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
            {
      
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    SpanLayoutBox spanBox = this.Document.GetLayoutBoxByPosition(e.Location) as SpanLayoutBox;
      
                    BuildContextMenu(spanBox).Show(this, e.Location);
      
                }
      
                base.OnMouseDown(e);
      
            }
      
      
        }
    }
  2. F11ADD2A-840D-4563-BD9F-564CF2DD488A
    F11ADD2A-840D-4563-BD9F-564CF2DD488A avatar
    2911 posts
    Member since:
    Apr 2022

    Posted 26 Mar 2013 Link to this post

    Hello Aaron,

    Thank you for sharing your code with the community. I have added 1500 
    Telerik Points to your account for your time and effort.
     

    Regards,
    Stefan
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. 42A3BB5D-8598-48FB-8064-04B1066455D5
    42A3BB5D-8598-48FB-8064-04B1066455D5 avatar
    12 posts
    Member since:
    Dec 2012

    Posted 02 Aug 2013 Link to this post

    Hi Telerik Team,

    I am trying to implement the above piece of code in VB.NET, however when i build my context menu the menuItem.Click is not found in the namespaces? Am I missing a subtle step, please provide an example in VB.NET for the following three lines of code . 

    menuItem = new RadMenuItem("Cut");
    menuItem.Click += (object sender, EventArgs e) => { this.Cut(); };
    menu.Items.Add(menuItem);

    Thank You,
    Nav
  4. 24667C81-31F8-4485-B7D7-FD5EB21D09B5
    24667C81-31F8-4485-B7D7-FD5EB21D09B5 avatar
    2 posts
    Member since:
    Jan 2012

    Posted 05 Aug 2013 Link to this post

    Hi Nav,

    Adding event handlers in VB.NET is a bit different, it would be something like:

    menuItem = New RadMenuItem("Cut")
    AddHandler menuItem.Click, Sub(sender As Object, e As EventArgs)
                                   Me.Cut()
                               End Sub
    Menu.Items.Add(menuItem)

    -Aaron
  5. F11ADD2A-840D-4563-BD9F-564CF2DD488A
    F11ADD2A-840D-4563-BD9F-564CF2DD488A avatar
    2911 posts
    Member since:
    Apr 2022

    Posted 06 Aug 2013 Link to this post

    Hello,

    That is correct. Here is an article about this: http://msdn.microsoft.com/en-us/library/6yyk8z93(v=vs.90).aspx.

    I hope this helps.
     

    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    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 >>
  6. 2AF9059D-2A0D-4E70-8BDA-62CED1E2CAC3
    2AF9059D-2A0D-4E70-8BDA-62CED1E2CAC3 avatar
    1 posts
    Member since:
    Mar 2013

    Posted 17 Mar 2015 Link to this post

    Hello,

    I have a problem with this code.
    The location used in the OnMouseDown event is the location on the screen.
    When the document is scrolled up the location is not correct.

    I tried to correct the Y position by using the vertical scroll position and that works most of the time as long as the scrollbar is used to scroll.
    If the mousewheel is used or keys like PageUp or Down then the position of the scrollbar does not always keep track.

    Is there a solution for getting the right position here? 

    Regards, Nico.
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.