Zebra0.com

csharp richtext

Using the Clipboard

The code below implements copy, cut and paste using the clipboard.
It is very important to add the code for the menu items because the menu items have the standard shortcuts of Ctrl+X, Ctrl+V, and Ctrl+C.

private void pasteToolStripButton_Click(object sender, EventArgs e)
{
   // Using the button on toolbar.
   Paste();
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
   // This is very important because the shortcut key for the menu item is Ctrl+V
   Paste();
}

private void Paste()
{
   // Paste from Clipboard
   richTextBox1.SelectedText = Clipboard.GetText();
   changed = true;
}

private void Cut()
{
   // Cut copys to clipboard and then deletes.
   Clipboard.SetText(richTextBox1.SelectedText);
   richTextBox1.SelectedText = "";
   changed = true;
}

private void Copy()
{
   // Copys to clipboard, no change to richtext
   Clipboard.SetText(richTextBox1.SelectedText);
   richTextBox1.SelectedText = "";
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
   // This is very important because the shortcut key for the menu item is Ctrl+X
   Cut();
}

private void cutToolStripButton_Click(object sender, EventArgs e)
{
   // Cut using button
   Cut();
}

private void copyToolStripButton_Click(object sender, EventArgs e)
{
   Copy();
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
   Copy();
}

To Do: Make sure this works before proceeding.

End of lesson, Next lesson: That is the last C# lesson in this course.