Enable copy, cut, past window in a rich text box
If you have more than one RichTextBox then you can use this extension method:
public static void AddContextMenu(this RichTextBox rtb)
{
if (rtb.ContextMenuStrip == null)
{
ContextMenuStrip cms = new ContextMenuStrip()
{
ShowImageMargin = false
};
ToolStripMenuItem tsmiUndo = new ToolStripMenuItem("Undo");
tsmiUndo.Click += (sender, e) => rtb.Undo();
cms.Items.Add(tsmiUndo);
ToolStripMenuItem tsmiRedo = new ToolStripMenuItem("Redo");
tsmiRedo.Click += (sender, e) => rtb.Redo();
cms.Items.Add(tsmiRedo);
cms.Items.Add(new ToolStripSeparator());
ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut");
tsmiCut.Click += (sender, e) => rtb.Cut();
cms.Items.Add(tsmiCut);
ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
tsmiCopy.Click += (sender, e) => rtb.Copy();
cms.Items.Add(tsmiCopy);
ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste");
tsmiPaste.Click += (sender, e) => rtb.Paste();
cms.Items.Add(tsmiPaste);
ToolStripMenuItem tsmiDelete = new ToolStripMenuItem("Delete");
tsmiDelete.Click += (sender, e) => rtb.SelectedText = "";
cms.Items.Add(tsmiDelete);
cms.Items.Add(new ToolStripSeparator());
ToolStripMenuItem tsmiSelectAll = new ToolStripMenuItem("Select All");
tsmiSelectAll.Click += (sender, e) => rtb.SelectAll();
cms.Items.Add(tsmiSelectAll);
cms.Opening += (sender, e) =>
{
tsmiUndo.Enabled = !rtb.ReadOnly && rtb.CanUndo;
tsmiRedo.Enabled = !rtb.ReadOnly && rtb.CanRedo;
tsmiCut.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
tsmiCopy.Enabled = rtb.SelectionLength > 0;
tsmiPaste.Enabled = !rtb.ReadOnly && Clipboard.ContainsText();
tsmiDelete.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
tsmiSelectAll.Enabled = rtb.TextLength > 0 && rtb.SelectionLength < rtb.TextLength;
};
rtb.ContextMenuStrip = cms;
}
}
And use it like this: richTextBox1.AddContextMenu();
Screenshot:
A standard RichTextBox does not contain a context menu for cut, copy and paste. However, you can look at this article which has the complete code needed to implement your own!
Try with this code
UPDATED CODE:
private void richTextBox1_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);
richTextBox1.ContextMenu = contextMenu;
}
}
void CutAction(object sender, EventArgs e)
{
richTextBox1.Cut();
}
void CopyAction(object sender, EventArgs e)
{
Graphics objGraphics;
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();
}
}
if you want to copy paste with another application like notepad (without styles )
please replace following methods
void CopyAction(object sender, EventArgs e)
{
Clipboard.SetText(richTextBox1.SelectedText);
}
void PasteAction(object sender, EventArgs e)
{
if (Clipboard.ContainsText())
{
richTextBox1.Text
+= Clipboard.GetText(TextDataFormat.Text).ToString();
}
}