ContextMenuStrip created at runtime on an overrided RichTextBox

I created a custom ContextMenuStrip at runtime on an overrided RichTextBox control (to enable Cancel, Cut, Copy and Paste basic functions on my overrided RichTextBox) - since the RichTextBox doesn't implement this out-of-the-box.
At runtime, I do get my ContextMenuStrip popping up when I right click on my overrided RichTextBox and the menus work well, BUT when I move the mouse over the ContextMenuStrip, I don't see the selection tracker over the menus. Why so? And how can I fix this?

       
        private void CreateContextMenuStrip()
        {
            _cms = new ContextMenuStrip();
            _cms.Items.Add(new ToolStripLabel("Annuler", null, false, cms_Clicked, "mnuCancel"));
            _cms.Items.Add(new ToolStripSeparator());
            _cms.Items.Add(new ToolStripLabel("Couper", null, false, cms_Clicked, "mnuCut"));
            _cms.Items.Add(new ToolStripLabel("Copier", null, false, cms_Clicked, "mnuCopy"));
            _cms.Items.Add(new ToolStripLabel("Coller", null, false, cms_Clicked, "mnuPaste"));
            _cms.Items.Add(new ToolStripSeparator());
            _cms.Items.Add(new ToolStripLabel("Tout sélectionner", null, false, cms_Clicked, "mnuSelectAll"));
            _cms.Items["mnuSelectAll"].Width = 150;
            _cms.Opening += new CancelEventHandler(cms_Opening);
            this.ContextMenuStrip = _cms;
        }

        private void cms_Opening(object sender, CancelEventArgs e)
        {
            // check to see if we can cut/copy, paste anything; then enable/ disable menu items
            // as required 
            IDataObject iData = Clipboard.GetDataObject();
            _cms.Items["mnuPaste"].Enabled = iData.GetDataPresent(DataFormats.Text);
            if (this.SelectionLength == 0)
            {
                _cms.Items["mnuCut"].Enabled = false;
                _cms.Items["mnuCopy"].Enabled = false;
                _cms.Items["mnuSelectAll"].Enabled = true;
            }
            else
            {
                _cms.Items["mnuCut"].Enabled = true;
                _cms.Items["mnuCopy"].Enabled = true;
                _cms.Items["mnuSelectAll"].Enabled = false;
            }
            _cms.Items["mnuCancel"].Enabled = this.CanUndo;
        }

        private void cms_Clicked(object sender, EventArgs e)
        {
            if (Object.ReferenceEquals(sender, _cms.Items["mnuCancel"]))
                this.Undo();
            else if (Object.ReferenceEquals(sender, _cms.Items["mnuCut"]))
                this.Cut();
            else if (Object.ReferenceEquals(sender, _cms.Items["mnuCopy"]))
                this.Copy();
            else if (Object.ReferenceEquals(sender, _cms.Items["mnuPaste"]))
                this.Paste();
            else if (Object.ReferenceEquals(sender, _cms.Items["mnuSelectAll"]))
                this.SelectAll();
        }

Open in new window


This what it looks like when it opens (you can see the selection hightlight is missing even if the mouse is moving hover) :
A.jpg
Should technically look like this (you can see the selection hightlight is there when moving the mouse hover) :
B.jpg
Thanks,
Claude
jettaa2vr6Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
I can only assume that when you specify that you overrode the RichTextBox Control that means that you created a new component/user control which inherits from the RichTextBox and specifies it's own implementation; e.g. -

RichTextBoxEx.cs -
using System.ComponentModel;
using System.Windows.Forms;

namespace EE_Q29095197
{
    public partial class RichTextBoxEx : RichTextBox
    {
        public RichTextBoxEx()
        {
            InitializeComponent();
        }

        public RichTextBoxEx(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private void OnCmsClick(object sender, System.EventArgs e)
        {
            if (sender is ToolStripMenuItem)
            {
                var item = sender as ToolStripMenuItem;
                if (item.Equals(mnuCancel))
                    Undo();
                else if (item.Equals(mnuCopy))
                    Cut();
                else if (item.Equals(mnuCut))
                    Copy();
                else if (item.Equals(mnuPaste))
                    Paste();
                else if (item.Equals(mnuSelectAll))
                    SelectAll();
            }
        }

        private void OnCmsOpening(object sender, CancelEventArgs e)
        {
            var iData = Clipboard.GetDataObject();

            mnuPaste.Enabled = iData.GetDataPresent(DataFormats.Text);
            mnuCut.Enabled = SelectionLength != 0;
            mnuCopy.Enabled = SelectionLength != 0;
            mnuSelectAll.Enabled = SelectionLength == 0;
            mnuCancel.Enabled = CanUndo;
        }
    }
}

Open in new window

RichTextBoxEx.Designer.cs -
namespace EE_Q29095197
{
    partial class RichTextBoxEx
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this._cms = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.mnuCancel = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.mnuCut = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuCopy = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuPaste = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.mnuSelectAll = new System.Windows.Forms.ToolStripMenuItem();
            this._cms.SuspendLayout();
            this.SuspendLayout();
            // 
            // _cms
            // 
            this._cms.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuCancel,
            this.toolStripSeparator1,
            this.mnuCut,
            this.mnuCopy,
            this.mnuPaste,
            this.toolStripSeparator2,
            this.mnuSelectAll});
            this._cms.Name = "_cms";
            this._cms.Size = new System.Drawing.Size(166, 126);
            this._cms.Opening += new System.ComponentModel.CancelEventHandler(this.OnCmsOpening);
            // 
            // mnuCancel
            // 
            this.mnuCancel.Name = "mnuCancel";
            this.mnuCancel.Size = new System.Drawing.Size(165, 22);
            this.mnuCancel.Text = "Annuler";
            this.mnuCancel.Click += new System.EventHandler(this.OnCmsClick);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(162, 6);
            // 
            // mnuCut
            // 
            this.mnuCut.Name = "mnuCut";
            this.mnuCut.Size = new System.Drawing.Size(165, 22);
            this.mnuCut.Text = "Couper";
            this.mnuCut.Click += new System.EventHandler(this.OnCmsClick);
            // 
            // mnuCopy
            // 
            this.mnuCopy.Name = "mnuCopy";
            this.mnuCopy.Size = new System.Drawing.Size(165, 22);
            this.mnuCopy.Text = "Copier";
            this.mnuCopy.Click += new System.EventHandler(this.OnCmsClick);
            // 
            // mnuPaste
            // 
            this.mnuPaste.Name = "mnuPaste";
            this.mnuPaste.Size = new System.Drawing.Size(165, 22);
            this.mnuPaste.Text = "Coller";
            this.mnuPaste.Click += new System.EventHandler(this.OnCmsClick);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(162, 6);
            // 
            // mnuSelectAll
            // 
            this.mnuSelectAll.Name = "mnuSelectAll";
            this.mnuSelectAll.Size = new System.Drawing.Size(165, 22);
            this.mnuSelectAll.Text = "Tout sélectionner";
            this.mnuSelectAll.Click += new System.EventHandler(this.OnCmsClick);
            // 
            // RichTextBoxEx
            // 
            this.ContextMenuStrip = this._cms;
            this._cms.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ContextMenuStrip _cms;
        private System.Windows.Forms.ToolStripMenuItem mnuCancel;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private System.Windows.Forms.ToolStripMenuItem mnuCut;
        private System.Windows.Forms.ToolStripMenuItem mnuCopy;
        private System.Windows.Forms.ToolStripMenuItem mnuPaste;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private System.Windows.Forms.ToolStripMenuItem mnuSelectAll;
    }
}

Open in new window

Which when added to a form -
Form1.cs -
using System.Windows.Forms;

namespace EE_Q29095197
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29095197
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new RichTextBoxEx();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 0);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(284, 261);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.richTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private RichTextBoxEx richTextBox1;
    }
}

Open in new window

Produces the following results -

Context Menu of an empty RTB -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jettaa2vr6Author Commented:
Thanks saige! Using the designer to provide the ContextMenuStrip instead of creating it on the fly works perfectly.

Thanks again!
Claude
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.