开发者

Appending Contents of two RichTextbox as a single RichText string

I want to append the contents of two rich text boxes in a Windows Forms .Net application; say: stringText = richtextbox1.Rtf 开发者_JAVA技巧+ richtextbox2.Rtf; The stringText should be RTF text, which should have \rtf line once, having concatenated rich text.

The Clipboard is not in scope here.

Also, I'm curious, if we can de-merge them.


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 Selectand SelectedRtf. The only requirement here is, that you need to know, at what position you want to split.


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.

This would be:

RichTextBoxSource.Select(0,RichTextBoxSource.TextLength);
RichTextBoxTarget.SelectedRtf = richTextBoxSource.SelectedRtf;

It's easy and works fine. Hope it will help somebody:)


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.

''' <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


well since I can't comment Pawel anwser, I must add that besides his code:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜