2013-11-25 65 views
1

这里是我的代码:我的RichTextBox的剪切/复制/粘贴不剪切,复制或粘贴

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; 
    } 
} 

我有2个问题:

  • richTextbox2标记文本后,我需要模拟鼠标右键点击看到剪贴粘贴复制菜单。
  • 当我点击复制,我不能将它粘贴到任何地方,因为没有什么可以粘贴。我还没有测试剪切和粘贴选项,但在复制后它不起作用。
+1

当'CopyAction'被调用时,你设置剪贴板,然后将其清除。我相信那不是你的意思。 –

+0

@ColeJohnson是的,忽略了它。 –

+1

@ColeJohnson“它在代码的顶部”与它有什么关系?如果您清除剪贴板,当单击“复制”菜单项时数据将消失,那么您无需粘贴任何内容。 – Bit

回答

5

取出Clipboard.Clear();

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

你也可以使用一个RichTextBoxCopy()方法:

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

对于贴:

void PasteAction(object sender, EventArgs e) { 
    if (Clipboard.ContainsText(TextDataFormat.Rtf)) { 
     SendKeys.Send("^v"); 
    } 
} 
+2

打我也是...... -_- –

+1

现在确定它的工作,但粘贴只在richTextBox2控件上工作。如果我想让粘贴在任何其他窗口上工作,如在cgrome中或在我拥有或其他任何地方的文本文件中? –

+1

@DoronMuzar有点复杂,理论上你必须检测聚焦的窗口并发送一些WM_PASTE消息到窗口。 –

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(); 
} 

注意:Windows窗体已内置方法将文本从richTextBox复制到剪贴板。 像复制,粘贴,剪切(你必须首先选择你想要复制或剪切的文本。在我的例子中,我已经给出了全选文本)。

在这个例子中,基本上它会将内容复制到剪贴板,仍然有重载方法,请参阅方法的定义。

2

尝试添加toolstripbar,添加3 toolstripbuttons。这是复制,剪切和粘贴的代码

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"); 
} 

该代码直接与剪贴板一起工作。

0

谢谢您的回答多伦先生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(); 
    }