Customized ContextMenuStrip and Spellchecker ContextMenuStrip at the same time

4 posts, 0 answers
  1. Dennis
    Dennis avatar
    7 posts
    Member since:
    Aug 2011

    Posted 21 Jan 2015 Link to this post

    Hello everybody,

    at first, the controls involved in the problem are the following:
    - RadTextBox
    - RadSpellchecker (spell check as you type mode)
    - ContextMenuStrip 

    I want to use a RadTextBox within a customized ContextMenuStrip and a RadSpellchecker. The problem is, that the RadSpellchecker overwrites the customized ContextMenuStrip with its own ContextMenuStrip if there are wrong words.
    Have you an idea how to use the customized ContextMenuStrip and the RadSpellchecker ContextMenuStrip at the same time?

    Many thanks in advance for your help
    Dennis
  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 26 Jan 2015 Link to this post

    Hello Dennis,

    Thank you for writing.

    Instead of using a ContextMenuStrip, I would recommend you to use a RadContextMenu. You need to subscribe to the RadTextBox.TextBoxElement.TextBoxItem.HostedControl.MouseDown event and show the RadContextMenu as demonstrated in Menus >> Context Menus help article. However, if there are misspelled words under the mouse the respective spell-checking pop up will be displayed which is desired behavior. You can prevent it from showing by canceling the TextBoxSpellChecker.DropDownMenu.PopupOpening event. Here is a sample code snippet demonstrating how to show a RadContextMenu on right mouse down when now errors are available: 
    public Form1()
    {
        InitializeComponent();
       
        this.radSpellChecker1.AutoSpellCheckControl = this.radTextBox1;
        this.radTextBox1.TextBoxElement.TextBoxItem.HostedControl.MouseDown += HostedControl_MouseDown;
        this.radTextBox1.TextBoxElement.TextBoxItem.HostedControl.ContextMenuStrip = this.contextMenuStrip1;
     
        TextBoxSpellChecker tbSpellChecker = this.radSpellChecker1.GetControlSpellChecker(typeof(RadTextBox)) as TextBoxSpellChecker;
        if (tbSpellChecker != null)
        {
            tbSpellChecker.DropDownMenu.PopupOpening += DropDownMenu_PopupOpening;
        }
    }
     
    private void HostedControl_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            //if no errors exist, show the RadContextMenu
            TextBoxSpellChecker tbSpellChecker = this.radSpellChecker1.GetControlSpellChecker(typeof(RadTextBox)) as TextBoxSpellChecker;
            Regex wordParser = new Regex("[\\p{L}\\p{N}\\p{M}]+(?:[-.'´_@][\\p{L}|\\p{N}|\\p{M}]+)*", RegexOptions.Compiled);
            Telerik.WinControls.RichTextBox.Proofing.DocumentSpellChecker checker = tbSpellChecker.SpellChecker
            as Telerik.WinControls.RichTextBox.Proofing.DocumentSpellChecker;
            bool containsError = false;
     
            foreach (Match word in wordParser.Matches(this.radTextBox1.Text + ""))
            {
                if (!checker.CheckWordIsCorrect(word.Captures[0].Value))
                {
                    containsError = true;
                    break
                }
            }
            if (!containsError)
            {
                Point p = (sender as Control).PointToScreen(e.Location);
                radContextMenu1.Show(p.X, p.Y);
            }
        }
    }
     
    private void DropDownMenu_PopupOpening(object sender, CancelEventArgs args)
    {
        //cancel it if you do not want the pop up with suggestions to show
        args.Cancel = true;
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dennis
    Dennis avatar
    7 posts
    Member since:
    Aug 2011

    Posted 03 Feb 2015 in reply to Dess Link to this post

    Dear Desislava,

    thank you for your answer. It helps me allmost for the hole problem. It leaves just a litte problem, maybe you can help me.
    I tried manny ways, but everything fails.

    For a better understanding, I added the code of my textbox below:

    001.using System;
    002.using System.Collections.Generic;
    003.using System.ComponentModel;
    004.using System.Drawing;
    005.using System.Data;
    006.using System.Linq;
    007.using System.Text;
    008.using System.Windows.Forms;
    009.using Telerik.WinControls.UI;
    010.using System.Text.RegularExpressions;
    011. 
    012.namespace Dennis.Controls
    013.{
    014.    public partial class TextBox : Telerik.WinControls.UI.RadTextBox
    015.    {
    016.        #region Variablen
    017.        private Telerik.WinControls.UI.RadSpellChecker spellchecker = new RadSpellChecker();
    018.        private bool _IsSpellcheckAktiv = false;
    019.        #endregion Variablen
    020. 
    021.        #region Propertys
    022.        /// <summary>
    023.        /// Activates or inaktivates the spellcheck option for the textbox.
    024.        /// </summary>
    025.        public bool IsSpellcheckAktiv
    026.        {
    027.            get { return this._IsSpellcheckAktiv; }
    028.            set
    029.            {
    030.                if (this._IsSpellcheckAktiv != value)
    031.                {
    032.                    this._IsSpellcheckAktiv = value;
    033.                    this.SpellcheckActivation();
    034.                }
    035.            }
    036.        }
    037.        #endregion Propertys
    038. 
    039.        #region Constructors
    040.        /// <summary>
    041.        /// Standard constructor
    042.        /// </summary>
    043.        public TextBox()
    044.        {
    045.            InitializeComponent();
    046. 
    047.            this.TextBoxElement.Border.ForeColor = System.Drawing.Color.FromArgb(156, 189, 232);
    048.        }
    049.        #endregion Constructors
    050. 
    051.        #region Events
    052.        /// <summary>
    053.        ///
    054.        /// </summary>
    055.        /// <param name="sender"></param>
    056.        /// <param name="e"></param>
    057.        private void TextBox_KeyDown(object sender, KeyEventArgs e)
    058.        {
    059.        }
    060. 
    061.        void DropDownMenu_PopupOpening(object sender, CancelEventArgs args)
    062.        {
    063.            //Adding a costumized menu to the spellcheck menu
    064.            (sender as RadItemsPopupControl).Items.Add(new RadMenuSeparatorItem());
    065. 
    066.            Telerik.WinControls.UI.RadMenuItem radmenuitem = new RadMenuItem() { Name = "rmi_Copy", Text = "Copy" };
    067.            radmenuitem.Click += new EventHandler(con_Copy_Click);
    068.            (sender as RadItemsPopupControl).Items.Add(radmenuitem);
    069. 
    070.            radmenuitem = new RadMenuItem() { Name = "rmi_Paste", Text = "Paste" };
    071.            radmenuitem.Click += new EventHandler(con_Paste_Click);
    072.            (sender as RadItemsPopupControl).Items.Add(radmenuitem);
    073.        }
    074. 
    075.        void HostedControl_MouseDown(object sender, MouseEventArgs e)
    076.        {
    077.            if (e.Button == System.Windows.Forms.MouseButtons.Right)
    078.            {
    079.                Telerik.WinControls.UI.TextBoxSpellChecker tbSpellchecker = this.spellchecker.GetControlSpellChecker(typeof(RadTextBox)) as Telerik.WinControls.UI.TextBoxSpellChecker;
    080.                Regex wordParser = new Regex("[\\p{L}\\p{N}\\p{M}]+(?:[-.'´_@][\\p{L}|\\p{N}|\\p{M}]+)*", RegexOptions.Compiled);
    081.                Telerik.WinControls.RichTextBox.Proofing.DocumentSpellChecker checker = tbSpellchecker.SpellChecker as Telerik.WinControls.RichTextBox.Proofing.DocumentSpellChecker;
    082.                bool containsError = false;
    083. 
    084.                int i = this.SelectionStart;
    085.                foreach (Match word in wordParser.Matches(this.Text + ""))
    086.                {
    087.                    if (!checker.CheckWordIsCorrect(word.Captures[0].Value))
    088.                    {
    089.                        containsError = true;
    090.                        break;
    091.                    }
    092.                }
    093. 
    094.                if (!containsError)
    095.                {
    096.                    Point p = (sender as Control).PointToScreen(e.Location);
    097.                    this.con_CopyAndPaste.Show(p);
    098.                }
    099.            }
    100.        }
    101. 
    102.        private void con_Paste_Click(object sender, EventArgs e)
    103.        {
    104.            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
    105.            {
    106.                this.TextBoxElement.TextBoxItem.Paste();
    107.            }
    108.        }
    109. 
    110.        private void con_Copy_Click(object sender, EventArgs e)
    111.        {
    112.            if (this.TextBoxElement.TextBoxItem.SelectionLength == 0)
    113.                this.TextBoxElement.TextBoxItem.SelectAll();
    114.            this.TextBoxElement.TextBoxItem.Copy();
    115.        }
    116. 
    117.        private void TextBox_Initialized(object sender, EventArgs e)
    118.        {
    119.            this.TextBoxElement.TextBoxItem.HostedControl.ContextMenuStrip = this.con_CopyAndPaste;
    120. 
    121.            this.SpellcheckActivation();
    122.        }
    123.        #endregion Events
    124. 
    125.        #region Methods
    126.        private void SpellcheckActivation()
    127.        {
    128.            if (this.IsSpellcheckAktiv)
    129.            {
    130.                this.spellchecker.AutoSpellCheckControl = this;
    131.                this.TextBoxElement.TextBoxItem.HostedControl.MouseDown += new MouseEventHandler(HostedControl_MouseDown);
    132. 
    133.                Telerik.WinControls.UI.TextBoxSpellChecker tbs = this.spellchecker.GetControlSpellChecker(typeof(RadTextBox)) as Telerik.WinControls.UI.TextBoxSpellChecker;
    134.                if (tbs != null)
    135.                {
    136.                    tbs.DropDownMenu.PopupOpening += new RadPopupOpeningEventHandler(DropDownMenu_PopupOpening);
    137.                }
    138.            }
    139.        }
    140.        #endregion Methods
    141.    }
    142.}

    These are the possible szenarios:
    1. All typed words are correct:
        - right click in the textbox shows a context menu with copy/paste button -> works
    2. E.g. one of five words is miss spelled:
        - right click on the miss spelled word shows a context menu of the spellchecker + copy/paste button -> works
        - right click on a right spelled word shows just the context menu with the copy/paste button -> fails

    I hope my explanaition is understandable :-)

    Best regards,
    Dennis
  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 06 Feb 2015 Link to this post

    Hello Dennis,

    Thank you for writing back.

    I have prepared a sample project demonstrating how to cover all of the described scenarios. Please refer to the attached zip file. Additionally, the provided gif file illustrates the implemented functionality.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top