1

I have a richtextbox and ContextMenuStrip which have cut, copy, past and select all they all work with no probelm but now I tried to add word suggestion like if the user select a word and right click on the richtextbox it should show him\her:

  • word suggestion
  • word suggestion
  • word suggestion
  • ...etc
  • "Line break"
  • Cut
  • Copy
  • Past
  • "Line break"
  • Select all

this is how it should be but the problem is suggestions list keeps duplicate itself with each right click(after I select the word)

here is the code:

ContextMenuStrip cms = new ContextMenuStrip { ShowImageMargin = true };

public void AddContextMenu(RichTextBox rtb)
    {
       if (rtb.ContextMenuStrip == null)
        {
            ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut");
            tsmiCut.Image = msg.Properties.Resources.cut;
            tsmiCut.Click += (sender, e) => rtb.Cut();
            cms.Items.Add(tsmiCut);
            ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
            tsmiCopy.Image = msg.Properties.Resources.copy;
            tsmiCopy.Click += (sender, e) => rtb.Copy();
            cms.Items.Add(tsmiCopy);
            ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste");
            tsmiPaste.Image = msg.Properties.Resources.paste;
            tsmiPaste.Click += (sender, e) => rtb.Paste();
            cms.Items.Add(tsmiPaste);

            cms.Items.Add("-");
            ToolStripMenuItem sALL = new ToolStripMenuItem("Select All");
            sALL.Image = msg.Properties.Resources.select_all;
            sALL.Click += (sender, e) => rtb.SelectAll();
            cms.Items.Add(sALL);
            rtb.ContextMenuStrip = cms;
        }
    }

private void richTextBox_MouseDown(object sender, MouseEventArgs e)
    {
        Hunspell hunspell = new Hunspell("en_US.aff", "en_US.dic");
        hunspell.Spell(richTextBox.SelectedText);
        List<string> suggestions = hunspell.Suggest(richTextBox.SelectedText);

        ToolStripSeparator line = new ToolStripSeparator();
        AddContextMenu(richTextBox);


        if (e.Button == MouseButtons.Right)
        {
            foreach (string suggestion in suggestions)
            {
                ToolStripMenuItem sugg = new ToolStripMenuItem(suggestion);
                if (cms.Items.Contains(sugg))
                {
                    cms.Items.Remove(sugg);
                }
                else
                {
                    cms.Items.Add(sugg);
                }
            }
        }

    }
H.Kathiri
  • 35
  • 4

1 Answers1

0

You need a contract to distinguish between the suggestion menu items and rest of menu items, then when adding suggestions, first remove existing suggestion items, then add new items.

Here as an example, I use Tag property of the ToolStripMenuItem as a contract, and all the menu strip item having suggestion in their tag are considered as suggestion:

public void Suggest(List<string> words, ContextMenuStrip menu)
{
    string suggestion = "suggestion";
    menu.Items.Cast<ToolStripItem>().Where(x => x.Tag == (object)suggestion)
        .ToList().ForEach(x => menu.Items.Remove(x));

    words.ToList().ForEach(x =>
    {
        var item = new ToolStripMenuItem(x);
        item.Tag = suggestion;
        item.Click += (s, e) => MessageBox.Show(x);
        menu.Items.Insert(0, item);
    });
}

And as usage, for one word:

Suggest(new List<string> { "something", "something else" }, contextMenuStrip1);

For another word:

Suggest(new List<string> { "another", "another one" }, contextMenuStrip1);
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • What about the line break after the list? – H.Kathiri Jan 17 '19 at 06:56
  • Just keep it as is. It's enough to have some items in the `ContextMenu`, like separator or some other menu items. Above code just removes existing "suggestion" items and add new "suggestion" items. It will not touch other items. – Reza Aghaei Jan 17 '19 at 07:07