I've a RichTextBox and I want color text in it. Is there any tag option? I'd like something like this [color:red]nick[/color] some message. Because I need to save it as text and I want on reload have also colored text.
Can I do something like this without writing own method?
You can set color for text in RichTextBox with SelectionColor
And if you want to save your rtf as plain text, then you will have to look at rtf format. Example:
{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line \cf2 This line is red\line \cf1
This line is the default color }
EDIT:
From this example - first of all you have to declare color table \colortbl in fololowing format:
{\colortbl; color1; color2; ... ; colorN;}
And then in the text you will have to enclose text with {\cfN YOUR_TEXT} where N is a number of color from table; you can not specify the boundaries of the block {}, then everything after \ cfN will be one color.
As the name says RichTextBox contains RichText
to change the Rtf text with 'rtf specific-tags' you can set/use the
RichTextBox.RtfProperty
also take a look at RichTextBox.SelectionColor to color text patterns in code
but when you don't want to use rtf, you said
need to save as text.
you could write your own 'markup' there is no built in expect rtf/html?
but rtf is text - at all
Example to use RichTextBix.SelectionColor to Color the text
richTextBox1.Text = "Hello";
richTextBox1.Select(0,2);
richTextBox1.SelectionColor = Color.Red;
colors the start of "Hello" red
and now you can access the 'taggeg' text in the RTFProperty of the RichTextBox
If you need examples of how things are encoded in RTF, you can create the document manually in Word or Wordpad, and save it as RTF. This will give you a hint about how to encode your formatting. Furthermore, if you're for instance creating help-documents, you can include them as an embedded resource and load them directly into the RichTextBox, with all the formatting included.
rtfMain.SaveFile(dlgSave.FileName);
From Reference Save text from rich text box with C#
Related
I am trying to inset formatted footnotes into an open word document using a WinForms application.
While I am able to use Interop.Word to set plain text footnotes and so long as I use plain text it works fine. However, I also want the user to be able to paste rich text formatted text from a rich text box into the footnote. This never works and always shows the rich text codes.
I know that footnotes can take formatting because if I put the rich text int a clipboard and paste it into a footnote the formatting is preserved.
I have even tried putting the rich text into the clipboard and then setting the string (s) to the clipboard contents using "s = Clipboard.GetText(TextDataFormat.Rtf);" It seems as if this should be exactly what I am pasting, but if I paste into the footnote it works. If the program sets it using the code below it does not work.
I appreciate any help.
application = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
application.ActiveDocument.Footnotes.Add(application.Selection.Range, "", s);
Cindy's answer was helpful. I fixed the problem by doing the following:
Saving the current selected position in the document and current clipboard contents.
Inserting a blank footnote.
Selecting the footnote.
Putting the string into the clipboard as rich text format.
Sending/pasting the information from the clipboard to Word.
Restoring the clipboard and original selected position in the document.
I want to replace some text in a docx file, and i'm using XCeed Docx lib, i want to ask if there is any way to replace the text with a new formatted text? For exemple i have the %value% (normal text) and i want to replace it with bold text to replace only the text i use the function document.ReplaceText(%value%, "Bold text"). Remark not all the replace text need to be bold and i do not know if the next is bold or not, i need the bold text only in some situations.
try this:
Formatting formatting = new Formatting();
formatting.Bold = true;
doc.ReplaceText("%value%", "Bold Text", false, System.Text.RegularExpressions.RegexOptions.None, formatting);
I'm working on a C# application (windows form) which has a DataGridView which is saved in a database (each cell considered as a string). What I want to do is to add a button that will color the text (or the cell BackColor) in such a way that when I print (on a paper) my database content, the color stays.
What I was thinking is to convert the cell string to RTF but I don't really know how to do this for the cells do not support RTF natively. I'd like to avoid as much as possible changing the DataGridView to a rich DataGridView so I was wondering if i can just do something like add this string when I save a colored cell:
"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fswiss\fprq2\fcharset0 Microsoft Sans Serif;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\ltrpar\cf1\f0\fs17 " + cell.Value.ToString() + "\cf0\f1\par"
The tricky thing is that when I open my application, I want to read each cell's string, and apply the RTF style to the cell style. For exemple I save a red cell which contains "hello", when I open my application I want the cell to be red, but when I print (paper again) I want the text to be red (so parse the RTF on open to separate the text from the format).
Any ideas? Thanks in advance. :)
Forgot to say I found the correct RTF string, it is the following:
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1036{\\fonttbl{\\f0\\fswiss\\fprq2\\fcharset0 Microsoft Sans Serif;}{\\f1\\fnil\\fcharset0 Microsoft Sans Serif;}}{\\colortbl ;\\red255\\green0\\blue0;}\\viewkind4\\uc1\\pard\\cf1\\f0\\fs17 " + value + "\\cf0\\f1}";
When starting the application I just check if the string cointains the RTF header, then I just split the text to keep the value and apply style to the cell.
I have a string that gets saved as rich text to a SQL field, so it has a lot of symbols in front of it like so...
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Verdana;}}
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22 Blah Blah Blah \par
I'm passing in this to Crystal Reports 11.5 as a parameter field, so the data type is string. I'm then just displaying it straight up by dragging the parameter to the report. The interpretation is set to RTF also in the paragraph tab of the formatting settings for this.
My problem is, it is removing the formatting tags but it is screwing up the text. It is randomly garbling words and inserting characters like the letter 'i' randomly in words, it's also putting some random letters in the background behind other words, making it look like it's been typed over manually if that makes sense, just makes the word look unreadable and bolded like someone typed over it with a typewriter.
I tried setting a new text object and then putting the rich text parameter in that to display, but then it doesn't remove/do any of the rich text formatting tags.
What gives? It can obviously do rich text as it removes the tags, but what is the deal with the random i's inserted into words and a couple of instances of the jumbled text over other words?
Unfortunately, I haven't used 11.5 yet.. but give this a try..
Format Field > Paragraph tab > Text Interpretation > RTF text
If the text is getting overlapped, then you can replace the following control set from your RTF text. It should work properly and not overlap.
data = data.Replace(#"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Verdana;}} {\colortbl ;\red0\green0\blue255;} \viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22 Blah Blah Blah \par", string.Empty);
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.
use the RTF property of the text box rather than the Text property...
richtextbox2.Rtf = richtextbox1.Rtf
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.
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?