Foresp
Foresp

Reputation: 425

How to move bold text to another richtextbox?

Okay, I will just leave my code here.

As you can see from that code, there is a button to make text bold, but not the whole text, just next things user is going to write.

For instance, when user types abc, clicks the button, types def: he gets: abc def.

But, when use: richtextbox2.text = richtextbox1.text;, richtextbox2.text value becomes abcdef, instead of abc def.

I want to copy exact text, including bold text.

Thanks.

Upvotes: 3

Views: 1167

Answers (3)

Tom Mallard
Tom Mallard

Reputation: 74

None of this works:

this.rtxtReport.Rtf = "{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 Tahoma;}}";
this.rtxtReport.Rtf += "{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}";
this.rtxtReport.Rtf += "{\\header\\pard\\qr\\plain\\f0\\chpgn\\par}";
this.rtxtReport.Rtf += "{\\pard{\\b ";
this.rtxtReport.Text += this.Ln + "> " + "VSTFS Report - " + System.DateTime.Now;
this.rtxtReport.Rtf += " \\b}\\par}";

The only way so far I've been able to bold text is to select it which isn't practical, I'm creating the doc from scratch, you'd have to select the text you're adding, did that, it did bold that text but also everything else added later!! ... the select(start, length) has length on it so a bust.

Anyone actually get C# to bold text by using rtf formatting and not having to select text?

Upvotes: 0

Basic
Basic

Reputation: 26766

Please award to essedbl as he deserves the points but another method which can come in handy with RTF boxes is to use the SelectedText property...

Specifically, set SelectionStart to be SomeRTFControl.Text.Length and SelectionLength to 0.

Then, set the SelectedText property to whatever you want and use the SelectionX properties to format the color, font, size, etc... of the text you're appending.

This doesn't result in visual artifacts/flickering but allows a very high level of control over text you're adding to an RTF programatically and also gives the usual strong-typing advantages.

Hope this helps.

Upvotes: 1

essedbl
essedbl

Reputation: 520

use the RTF property of the text box rather than the Text property...

richtextbox2.Rtf = richtextbox1.Rtf

Upvotes: 2

Related Questions