复制+粘贴到富文本框中/在其中? [英] copy + paste into/ within a rich textbox?

查看:315
本文介绍了复制+粘贴到富文本框中/在其中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用:

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

推荐答案

此控件没有明确的插入或追加.

这会将您的剪贴板文本插入光标或替换当前选择:

There are no explicit inserts or appends for this Control.

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

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的最终答案.
您可能还想记住并在添加后恢复先前的选择.使用上面显示的属性SelectionStartSelectionLength.


—SA



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


来源:
textBox1

目的地:
richTextBox1

Source:
textBox1

Destination:
richTextBox1

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);
}


这篇关于复制+粘贴到富文本框中/在其中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆