I have searched around on the net but I cannot find a method of copying/cutting/pasting selected text from a RichTextBox
.
Even MSDN does not have an answer. The code they provide does not work: Copy()
only appears to work on TextBoxes, not RichTextBoxes.
if I copy this method:
Clipboard.SetText(richTextBox1.SelectedRtf, TextDataFormat.Rtf);
i can't paste to notepad
if I copy this method:
Clipboard.SetText(richTextBox1.SelectedText, TextDataFormat.UnicodeText);
it's working in Word and notepad, but inserts in word without formating
richTextBox1.Copy();
working in Word and notepad, but I can't modify string value.
How can I copy normally formatted string in Clipboard?
P.S. I found
DataObject dto = new DataObject();
dto.SetText(mesrtf, TextDataFormat.Rtf);
dto.SetText(mes, TextDataFormat.UnicodeText);
Clipboard.Clear();
Clipboard.SetDataObject(dto);
it works
If you're using .NET 3.0 and above you can always use Clipboard.SetText()
I found it useful to use the Clipboard when I want everything in the richTextBox without having to select everything first or when I need to change the string:
string text = "Summary:" + Environment.NewLine + this.richTextBoxSummary.Text;
Clipboard.SetText(text);
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