1

Here's my code:

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

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

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

private void richTextBox2_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {   //click event
        //MessageBox.Show("you got it!");
        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);

        richTextBox2.ContextMenu = contextMenu;
    }
} 

I have 2 problems:

  • After marking the text in richTextbox2 I need to simulate a right click on the mouse to see the cut paste copy menu.
  • When I click on Copy I can't afterwards paste it anywhere because there is nothing to paste. I didn't test yet the cut and paste options but after doing copy it's not working.
Breeze
  • 2,010
  • 2
  • 32
  • 43
Doron Muzar
  • 443
  • 1
  • 10
  • 26
  • 1
    When `CopyAction` is called, you set the clipboard and then clear it. I'm sure that's not what you meant. – Cole Tobin Nov 25 '13 at 22:14
  • @ColeJohnson yes, overlooked it. – King King Nov 25 '13 at 22:15
  • 1
    @ColeJohnson what does "it's being at the top of the code" have to do with it? If you clear the clipboard the data is gone when the Copy menu item is clicked then you have nothing to paste. – Bit Nov 25 '13 at 22:20
  • 1
    @N4TKD it was a reply to King King who didn't notice the `CopyAction` method, so I said it was at the top of the code block. – Cole Tobin Nov 26 '13 at 00:40

4 Answers4

5

Remove the Clipboard.Clear();

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

You can also use the Copy() method of a RichTextBox:

void CopyAction(object sender, EventArgs e) {
 richTextBox2.Copy();
}

For paste:

void PasteAction(object sender, EventArgs e) {
   if (Clipboard.ContainsText(TextDataFormat.Rtf)) {
      SendKeys.Send("^v");
   } 
}
King King
  • 61,710
  • 16
  • 105
  • 130
2
private void btnCopy_Click(object sender, EventArgs e)
{
    richTextBox1.SelectAll();
    richTextBox1.Copy();
}

private void btnPaste_Click(object sender, EventArgs e)
{
    richTextBox2.Paste();
}

private void btnCut_Click(object sender, EventArgs e)
{
    richTextBox1.SelectAll();
    richTextBox1.Cut();
}

Note:The windows forms has built in methods to copy the text from richTextBox to Clipboard. like Copy, Paste, Cut (you have to select the text first which you want to copy or cut. Here in my example i had given select all text).

Here in this example basically it will copy the content to the clipboard, still there are overloading methods please see the definitions of the methods.

VDN
  • 320
  • 3
  • 8
2

Try to add toolstripbar, add 3 toolstripbuttons. This is the code for copy, cut and paste

private void toolStripButton1_Click(object sender, EventArgs e)
{
    SendKeys.Send("^x");
}

private void toolStripButton2_Click(object sender, EventArgs e)
{
    SendKeys.Send("^v");
}

private void toolStripButton3_Click(object sender, EventArgs e)
{
    SendKeys.Send("^c");
}

The code works directly with the clipboard.

Breeze
  • 2,010
  • 2
  • 32
  • 43
nassimlouchani
  • 423
  • 4
  • 8
0

thanks for your answer mr Doron Muzar

  private void richTextBox2_MouseUp(object sender, MouseEventArgs e)
   {
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
       {   //click event
           //MessageBox.Show("you got it!");
           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);

           richTextBox2.ContextMenu = contextMenu;
}

}

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

    void CopyAction(object sender, EventArgs e)
    {
        richTextBox1.Copy();

    }

    void PasteAction(object sender, EventArgs e)
    {`
        richTextBox1.Paste();
    }