Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy selected text from RichTextBox

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.

like image 376
nevada_scout Avatar asked Oct 31 '10 17:10

nevada_scout


2 Answers

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

like image 118
Drakosha Avatar answered Oct 13 '22 01:10

Drakosha


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);
like image 34
John Warlow Avatar answered Oct 13 '22 02:10

John Warlow