Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i use:
Clipboard.SetText(telep.Text);

to copy, and:
telep.Text = Clipboard.GetText();

to paste. but instead of adding onto the current context,it replaces everything in the rich textbox.
'telep' is the rich textbox.
thanks
Posted
Comments
Orcun Iyigun 18-Feb-11 16:12pm    
telep.Text = telep.Text + Clipboard.GetText(); ?
Sergey Alexandrovich Kryukov 18-Feb-11 16:34pm    
Thank you, I credited this suggestion in my answer, but as "pretty bad" (big performance problem), a better way exists.
--SA
Orcun Iyigun 18-Feb-11 16:56pm    
One of the reason I replied as a comment instead of an answer because I was unsure and yeah yours seem more efficient.
Sergey Alexandrovich Kryukov 18-Feb-11 17:20pm    
Yes, you're absolutely wise, unlike Vraith84 who essentially repeated the same (see).
Thanks again.
Respect,
--SA

There are no explicit inserts or appends for this Control.

This will insert your clipboard text at cursor or replace current selection:

C#
telep.SelectedText = Clipboard.GetText();


This will do append (credit to orc_orc_orc), but this operation can be very slow, as it will copy whole text of the control to some local memory and than copy a sum back to control. So this method is pretty bad:

C#
telep.Text = telep.Text + Clipboard.GetText();


Finally, the ultimate solution for append should combine cursor manipulation with SelectedText property.

C#
string newText = Clipboard.GetText();
telep.SelectionStart = telep.TextLength;
telep.SelectionLength = 0;
telep.SelectedText = newText;


This is very important to use the property TextLength, not Text.Length, otherwise it will again be very slow.

This is a final answer for append.
You may also want to remember and restore previous selection after appending. Use the properties SelectionStart and SelectionLength shown above.


—SA
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 16:32pm    
Thank you for accepting my answer. Please note: it was updated after you voted.
Good luck, call again.
--SA
vlad781 19-Feb-11 0:59am    
well, it still solved my problem :)
cheers
Espen Harlinn 19-Feb-11 12:02pm    
My 5 - good effort :)
Sergey Alexandrovich Kryukov 19-Feb-11 16:43pm    
Thank you very much. RichEdit has little subtle problems, it does.
--SA
Source:
textBox1

Destination:
richTextBox1

SQL
if (richTextBox1.SelectionLength > 0)
{
    //Replace selected text if there is any
    richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.SelectionStart, richTextBox1.SelectionLength).Insert(richTextBox1.SelectionStart, textBox1.Text);
} else {
    //simple insert
    richTextBox1.Text = richTextBox1.Text.Insert(richTextBox1.SelectionStart, textBox1.Text);
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 17:19pm    
Not quite relevant, and not acceptable by the same reasons. This is exactly equivalent to what ocr_ocr_ocr suggested, but he knew it was not good enough to post is as an answer. Also, you repeat already mentined method.
Sorry,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900