windows 将两个 RichTextbox 的内容附加为单个 RichText 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4994454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending Contents of two RichTextbox as a single RichText string
提问by iTSrAVIE
I want to append the contents of two rich text boxes in a Windows Forms .Net application; say: stringText = richtextbox1.Rtf + richtextbox2.Rtf;
The stringText
should be RTF text, which should have \rtf line once, having concatenated rich text.
我想在 Windows Forms .Net 应用程序中附加两个富文本框的内容;比如说:stringText = richtextbox1.Rtf + richtextbox2.Rtf;
在stringText
应RTF文本,里面应该有\ RTF线一次,已经串联富文本。
The Clipboard is not in scope here.
剪贴板不在此处的范围内。
Also, I'm curious, if we can de-merge them.
另外,我很好奇,我们是否可以将它们分开。
回答by Daniel Hilgarth
Try this:
尝试这个:
richTextBoxTarget.Select(richTextBoxTarget.TextLength, 0);
richTextBoxTarget.SelectedRtf = richTextBoxSource.Rtf;
This merges the contents of richTextBoxSource to the end of richTextBoxTarget. It automatically creates valid RTF with only one \rtf tag.
To de-merge, also use Select
and SelectedRtf
. The only requirement here is, that you need to know, at what position you want to split.
这会将richTextBoxSource 的内容合并到richTextBoxTarget 的末尾。它会自动创建只有一个 \rtf 标签的有效 RTF。
要拆分,还可以使用Select
和SelectedRtf
。这里唯一的要求是,您需要知道要拆分的位置。
回答by Pawe? Por?ba
I know it's old question, but it seems to be a common one. Thus, I'm gonna add my answer to this, cause the marked answer make RTF's to concatenate, but it also gives an extra new line, every time.
我知道这是个老问题,但这似乎是一个常见问题。因此,我将对此添加我的答案,导致标记的答案使 RTF 连接起来,但它每次都会提供一个额外的新行。
This would be:
这将是:
RichTextBoxSource.Select(0,RichTextBoxSource.TextLength);
RichTextBoxTarget.SelectedRtf = richTextBoxSource.SelectedRtf;
It's easy and works fine. Hope it will help somebody:)
这很容易并且工作正常。希望它会帮助某人:)
回答by GAltelino
well since I can't comment Pawel anwser, I must add that besides his code:
好吧,因为我无法评论 Pawel anwser,所以除了他的代码之外,我还必须补充一点:
RichTextBoxSource.Select(0,RichTextBoxSource.TextLength);
RichTextBoxTarget.SelectedRtf = richTextBoxSource.SelectedRtf;
you should add if you want the new text to be always on the top
如果您希望新文本始终位于顶部,则应该添加
RichTextBoxTarget.Select(0,0);
or if you want it to be always on the bottom
或者如果你希望它总是在底部
RichTextBoxTarget.Select(RichTextBoxTarget.TextLength,0);
So you also have control of the position like in Daniel answer, even if the target richtextbox is clickable.
因此,即使目标 Richtextbox 是可点击的,您也可以像 Daniel answer 一样控制位置。
回答by Daniel Neel
Not sure if this is useful, but here's the above code re-formatted into an extension method. This lets you say:
不确定这是否有用,但这里将上面的代码重新格式化为扩展方法。这让你说:
textBox.AppendRtf(someRtfString)
which handily matches up with the RichTextBox
class's AppendText()
method.
这很容易与RichTextBox
类的AppendText()
方法相匹配。
''' <summary>
''' Appends the provided RTF-formatted string to the provided <see cref="RichTextBox"/>.
''' </summary>
<Extension()> _
Public Sub AppendRtf(ByVal rtbTextBox As RichTextBox, ByVal strRtf As String)
rtbTextBox.Select(rtbTextBox.TextLength, 0)
rtbTextBox.SelectedRtf = strRtf
End Sub