Better Way of Manipulating RichText in C#?
Asked Answered
R

6

5

I need to create and copy to the clipboard some RichText with standard "formatting" like bold/italics, indents and the like. The way I'm doing it now seems kind of inelegant... I'm creating a RichTextBox item and applying my formatting through that like so:

RichTextBox rtb = new RichTextBox();
Font boldfont = new Font("Times New Roman", 10, FontStyle.Bold);
rtb.Text = "sometext";
rtb.SelectAll()
rtb.SelectionFont = boldfont;
rtb.SelectionIndent = 12;

There has got to be a better way, but after a few hours of searching I was unable to come up with anything better. Any ideas?

Edit: The RichTextBox (rtb) is not displayed/drawn anywhere on a form. I'm just using the object to format my RichText.

Roofdeck answered 25/9, 2008 at 21:5 Comment(0)
A
3

You may want to suspend the layout of the richtextbox before you do all of that, to avoid unecessary flicker. That's one of the common mistakes I used to make which made it seem "inelegant"

Accouchement answered 25/9, 2008 at 21:11 Comment(2)
Well that's the thing; The RichTextBox isn't even displayed anywhere...I'm just using it because I couldn't find an equally facile way to manipulate/format RichText. Thanks for your suggestion.Roofdeck
Well, I suppose you could create a whole class structure and methods and whatnot for directly manipulating rich text, and then you could post that code online so others could use it. A google search for rich text libraries turns up nothing?Accouchement
H
3

I think your technique is a great way to accomplish what you're looking to do. I know what you mean...it feels kind of "dirty" because you're using a Winforms control for something other than it was intended for, but it just works. I've used this technique for years. Interested to see if anyone else has viable options.

Hibiscus answered 25/9, 2008 at 21:23 Comment(2)
Agreed. It's no different from creating a WebBrowser control just to parse a page and get the text out or something, which I've done before. Nothing wrong with using tools for new purposes.Avatar
Ya, that's exactly what I'm trying to say...it just feels wrong...If there is no equally facile way to go about it I am fine...I would much rather do this than mess around trying to format strings into RTF manually...I just thought there must be something out there that I'm missing.Roofdeck
W
3

Is this project helpful?

http://www.codeproject.com/KB/string/nrtftree.aspx

Wensleydale answered 5/6, 2009 at 17:50 Comment(0)
S
2

You could create some utility extension methods to make it more 'elegant' :)

public static RichTextBox Set(this RichTextBox rtb, Font font, string text)
{               
    rtb.Text = text;
    rtb.SelectAll();
    rtb.SelectionFont = font;
    rtb.SelectionIndent = 12;
    return rtb;
}

And call like this:

someRtb.Set(yourFont, "The Text").AndThenYouCanAddMoreAndCHainThem();

Edit: I see now that you are not even displaying it. Hrm, interesting, sorry i wasnt of much help with providing a Non Rtb way.

Scanlan answered 25/9, 2008 at 21:19 Comment(1)
This is good but should be coupled with the @hova's suggestion to suspend redrawing while it's happening.Airedale
D
1

You should also suspend the layout of the richtextbox just after creation and dispose it after use.

RichTextBox rtb = new RichTextBox();
rtb.SuspendLayout();
//richtext processing goes here...
rtb.Dispose();

And don't be shy to use richtextbox for richtext processing. Microsoft itself is doing this here in this tutorial. :-)

Diadelphous answered 5/11, 2014 at 23:10 Comment(0)
W
0

I know it's been a while, but check out this stackoverflow post on converting rtf to html. It would probably be way easier to get your stuff into html, manipulate it, then either display it using html or convert it back to rtf.

Convert Rtf to HTML

Wensleydale answered 5/9, 2011 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.