0
votes

What i tried now is in the richTextBox1 mouseup event and also the events for each action:

private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
                MenuItem menuItem = new MenuItem("Cut");
                menuItem.Click += new EventHandler(CutAction);
                contextMenu.MenuItems.Add(menuItem);
                menuItem = new MenuItem("Copy");
                menuItem.Click += new EventHandler(CopyAction);
                contextMenu.MenuItems.Add(menuItem);
                menuItem = new MenuItem("Paste");
                menuItem.Click += new EventHandler(PasteAction);
                contextMenu.MenuItems.Add(menuItem);

                richTextBox1.ContextMenu = contextMenu;
            }
        }

        void CutAction(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        void CopyAction(object sender, EventArgs e)
        {
            Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);
            Clipboard.Clear();
        }

        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                richTextBox1.SelectedRtf
                    = Clipboard.GetData(DataFormats.Rtf).ToString();
            }
        }

There are two problems:

  1. When i mark text in the rcihTextbox and make right click nothing happen only when i make another right click i see the menu Cut Copy Paste. Why it's not showing the menu on the first right click ?

  2. Second problem when i make Copy click on Copy then i go to the chrome browser and try to make paste it's empty the paste is empty like it didn't copy it at all.

I checked now again only Cut is working. If i make Copy it's not copying anything and i can't paste into Chrome address bar at top. Or if i copied from exmaple from chrome something i searched for: hello world then the Paste in the richTextBox is empty.

I want to be able to copy/cut/paste from inside the richTextBox control it self and from other external programs like notepad chrome ie or even other richtextbox control.

1

1 Answers

0
votes

The ContextMenu is not showing because you set it after the right click. Try to set it on the MouseDown event. When you click copy, you set the text in the clipboard but you remove it after with the Clipboard.Clear(), so i don't know what you were thinking by writing this line, just remove it.