Right Click on RichTextBox And Copy/Paste

RwCC 0 Tallied Votes 2K Views Share

Something that we normally have to do at one point is right click on a richtextbox etc to copy/paste. Now, we all know there are keyboard shortcuts but for the end user it is better to have this feature. Here is the basic code to allow you to add this feature. Of course others can be added to give increased functionality.

I hope this helps :)

MenuItem mi = new MenuItem("Cut");
            mi.Click += new EventHandler(mi_Cut);
            cm.MenuItems.Add(mi);

            mi = new MenuItem("Copy");
            mi.Click += new EventHandler(mi_Copy);
            cm.MenuItems.Add(mi);

            mi = new MenuItem("Paste");
            mi.Click += new EventHandler(mi_Paste);
            cm.MenuItems.Add(mi);
            
            richTextBox.ContextMenu = cm;




//Code to add right click

        void mi_Cut(object sender, EventArgs e)
        {
            richTextBox..Cut();
        }
        void mi_Copy(object sender, EventArgs e)        
        {
            Graphics objGraphics;
            Clipboard.SetData(DataFormats.Rtf, richTextBox..SelectedRtf);
            Clipboard.Clear();
        }

        void mi_Paste(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                richTextBox.SelectedRtf = Clipboard.GetData(DataFormats.Rtf).ToString();
            }
        }
[/CODE]
hewllo56 0 Newbie Poster

it says, "Error 1 The name 'cm' does not exist in the current context"

and

Warning 5 The variable 'objGraphics' is declared but never used

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.