I have a rich text box(richTextBox1
) in my program as shown bellow. But when I right click on it, it doesn't pop up a “copy, cut, past” window. Can you please tell me how can I enable this “copy, cut, past” window in to my Rich Text Box? I am new to C#, please let me know step by step, if you know how to solve this
The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. This topic introduces the TextBox class and provides examples of how to use it in both Extensible Application Markup Language (XAML) and C#.
Step 1: Create a RichTextBox control using the RichTextBox() constructor is provided by the RichTextBox class. // Creating a RichTextBox control RichTextBox box = new RichTextBox();
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:
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With