问题描述
我使用:
Clipboard.SetText(telep.Text);
复制,然后:
to copy, and:
telep.Text = Clipboard.GetText();
粘贴.但它不会替换当前文本,而是替换了富文本框中的所有内容.
"telep"是富文本框.
感谢
to paste. but instead of adding onto the current context,it replaces everything in the rich textbox.
''telep'' is the rich textbox.
thanks
推荐答案
telep.SelectedText = Clipboard.GetText();
这会追加(贷记给orc_orc_orc),但是此操作可能非常慢,因为它将把控件的整个文本复制到某些本地内存中,而不是将总和复制回到控件中.所以这种方法很糟糕:
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:
telep.Text = telep.Text + Clipboard.GetText();
最后,追加的最终解决方案应将光标操作与SelectedText
属性结合起来.
Finally, the ultimate solution for append should combine cursor manipulation with SelectedText
property.
string newText = Clipboard.GetText();
telep.SelectionStart = telep.TextLength;
telep.SelectionLength = 0;
telep.SelectedText = newText;
这对于使用属性TextLength
而不是Text.Length
非常重要,否则将再次变得非常慢.
这是append的最终答案.
您可能还想记住并在添加后恢复先前的选择.使用上面显示的属性SelectionStart
和SelectionLength
.
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.
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);
}
这篇关于复制+粘贴到富文本框中/在其中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!