2013-11-25 268 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(); 
    } 
相關問題