This project is read-only.

colored text C#.Net

Topics: Developer Forum, Project Management Forum, User Forum
Jul 9, 2010 at 4:40 PM

hi everyone...

i've got a problem... i need to copy a text from a richtextbox... but this text have some colored words and when i copy this text in scintilla i lose the color!

also if i disable the configuration manager and the lexer the text become all black or if i insert a language in the lexing the text will take the color of the language!

how can i copy the text with my color too???

Jul 9, 2010 at 8:25 PM

These are all "by design". Scintilla is not a RichTextBox, it's a syntax highlighting code editor. A RichTextBox is pretty free-flowing. Any run of text can have any color or font variation. It's meant to have users apply these styles to any arbitrary run of text at any time. Additionally all this formatting is actually a part of the document. If you make a rich text file, save it, and send it to a friend it will look exactly the same to them. That's why it makes a good Word Processing component (WordPad) and a poor source code editor (too hard to apply consistent syntax highlighting rules)

On the other hand Scintilla is purely a text editor. It takes plain text as input and outputs plain text (yes extensions can make it export rtf and html but try opening it back up and see what you get). The fonts, colors, highlighting, folding etc are not a part of the document. They are all determined programmatically based upon rules. If you edit a document in Scintilla, save it, and send it to a friend they'll open it in whatever choice of editor they use. They may see it as plain black and white or whatever code editor will apply colors and fonts that he chooses. Scintilla controls have a fixed set of colors and fonts. These styles are applied to text automatically based upon the content of the text. That's why it's used for editing source code. It's not meant to edit Rich Text, when rtf is pasted Scintilla interprets it as plain text and tries to apply its formatting rules to it. If you don't have a lexer applied there are no rules to apply.

Now that's not to say that it isn't possible, just very difficult because you are trying to use a hammer as a screwdriver. If you are hard set on perusing this you can go about it this way:

  1. Don't use a lexer
  2. Intercept the paste to Scintilla
  3. Parse through the Rich Text looking for every unique color/background color/font (family size style) combination and calling this a "style"
  4. Record which style is applied to every range of text
  5. Set up a Scintilla Style for every item in step 3 (you're limited to 128 btw)
  6. Paste the text into Scintilla
  7. Go through all the ranges in step 4 and apply the scintilla styles from step 5

That list a monumentally difficult thing to do. And it's still problematic as RichText has other concepts like bullets, tabstops, line spacing and OLE embedded objects that either don't translate well to Scintilla or have no equivalent whatsoever.

Jul 10, 2010 at 10:49 AM

Ok... i'm really scared by this list... i think i'll choose another way for paste the text with the color... because i don't know how to do the list you wrote... if have you got something for do this contact me else nothing!

Thank you anyway!