简体   繁体   English

如何在Richtextbox控件中进行复制剪切粘贴?

[英]How can i make copy cut paste in richtextbox control ?

What i tried now is in the richTextBox1 mouseup event and also the events for each action: 我现在尝试的是在richTextBox1 mouseup事件以及每个动作的事件中:

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. 当我在rcihTextbox中标记文本并单击鼠标右键时,只有当我再次单击鼠标右键时,什么也不会发生,我会看到菜单“剪切复制粘贴”。 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. 当我进行“复制”时,第二个问题是单击“复制”,然后我进入chrome浏览器并尝试将其粘贴为空。粘贴为空,因为它根本没有将其复制。

I checked now again only Cut is working. 我现在再次检查,只有Cut在工作。 If i make Copy it's not copying anything and i can't paste into Chrome address bar at top. 如果我执行“复制”操作,则不会复制任何内容,也无法粘贴到顶部的Chrome地址栏中。 Or if i copied from exmaple from chrome something i searched for: hello world then the Paste in the richTextBox is empty. 或者,如果我从chrome的exmaple复制了我搜索过的内容:世界,那么richTextBox中的粘贴为空。

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. 我希望能够从richTextBox控件自身内部以及从其他外部程序(如记事本chrome ie甚至其他richtextbox控件)复制/剪切/粘贴。

The ContextMenu is not showing because you set it after the right click. 由于右键单击后进行了设置,因此未显示ContextMenu。 Try to set it on the MouseDown event. 尝试在MouseDown事件上进行设置。 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. 当您单击复制时,您在剪贴板中设置了文本,但是在使用Clipboard.Clear()之后将其删除了,所以我不知道您在写这行时的想法,只是删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM