3

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.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
Foresp
  • 425
  • 1
  • 7
  • 15
  • also when im posting a question it says typing text in "**"def"**"(Without "'s) it becomes italic, so read it as bold text please :) – Foresp May 02 '11 at 22:13
  • it works in comment mode. interesting... there was two stars between "'s – Foresp May 02 '11 at 22:14
  • @user683512 the text formatting sometimes has a hiccup if you don't leave whitespace between formatting and surrounding text. :) – Basic May 02 '11 at 22:45

3 Answers3

2

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

richtextbox2.Rtf = richtextbox1.Rtf
essedbl
  • 520
  • 1
  • 7
  • 19
  • it works, thank you. i will mark it as answer in 15 min :p (system wont let me do it now ^^) – Foresp May 02 '11 at 22:15
1

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.

Basic
  • 26,321
  • 24
  • 115
  • 201
0

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?

  • The generic rtftextbox overwrites the Rtf code after \ansi!! ... so you have to use selection and can't author Rtf code from scratch. Have selection working ok now. – Tom Mallard Mar 16 '12 at 00:05