I want to append text, for example "World" in a textbox, but not with the Windows.Forms functions for RichTextBox. Instead i want to specify position and the string that i want to insert into Rtf property of the RichTextBox! So, is it possible to update the rtf string directly ?
string rtfStuffs = this.richTextBox1.Rtf;
// Edit as you see fit...
this.richTextBox1.Rtf = rtfStuffs;
Related
I have a WPF Project (with MVVM Light) that contains a RichTextBox control with Toolbar like below image
I try to store the formatted text ( user can change rich text box content style to 'Bold','Italics','underline' or increase or decrease the font size ) to sql lite database.
I am using TextRange class to capture the RichTextBox content.
/* Get Richtext box text. */
TextRange range;
range = new TextRange(((FlowDocument)document).ContentStart,
((FlowDocument)document).ContentEnd);
/* Get Richtext box text. */
But it's not captured correct text if the text is bold or italics. I googled many hours, but i can't find a right method to my requirement. Please advice if anyone have idea.
I don't know where the formatting data is stored in relation to the rich text box properties, but the TextRange.Text property is just a raw string of text.
My approach would be the extract the raw RTF data and save/load this accordingly. This also has the advantages of being able to be opened in any other rich text editor such as Microsoft Word.
To get an RTF string representation you can use this code:
var ms = new MemoryStream();
var doc = RichTextBox.Document;
var range = new TextRange(doc.ContentStart, doc.ContentEnd);
range.Save(ms, DataFormats.Rtf);
string rtfString = ASCIIEncoding.Default.GetString(ms.ToArray());
//You can then save this string to the database or whatever you want...
and to load an RTF string back into the RichTextBox you can use this code.
string rtfText = LoadTextFromDatabase() //However you read the saved string..
var ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(rtfText));
RichTextBox.Selection.Load(ms, DataFormats.Rtf);
Hope this helps and is a suitable solution for you.
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);
My program reads a DDS image file and stores it as a byte array. I want to be able to show the users the raw data in a TextBox form so at first I convert the byte array to a string using the following code:
string data = System.Text.Encoding.ASCII.GetString(bytes);
I then set the TextBox text:
textBox.Text = data;
The problem I am having is the text box is not showing all the data. Here is a screenshot of how it looks:
As you can see only the first few characters are displayed. I am assuming this is because the string contains a null terminator which the TextBox interprets as the end of the string. Here is a copy paste of the first 50 or so characters in the string which I copied directly from the debugger watch window:
DDS |\0\0\0\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
As you can see the first null character comes right after "DDS |" which explains why that's all that shows up in the TextBox.
What I want to be displayed is similar to what you see if you edit the raw DDS file with a text editor such as Notepadd++.
Opening the DDS file in Notepad++ produces the following:
My question is, how do I get my TextBox (or RichTextBox) to show the data in the same way that Notepad++ shows it?
The simplest solution is to use this:
textbox.Text = data.Replace("\0", #"\0");
This will force the textbox to actually show a backslash followed by a zero where the nulls would be. Alternatively, you could replace the nulls with some other character or string.
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#
I have a text file in which I have a word like $variable. I read it all and look if the "$variable" is in it. Then I want to replace the word "$variable" to the content of a text box and afterwards write the text in the same text file back.
If I use constantly strings like
string var = "hello";
It works. But can anyone tell me how to do this with the content of a text box?
This might help:
How to write to a file
http://msdn.microsoft.com/en-us/library/6ka1wd3w
and how to read from a file
http://msdn.microsoft.com/en-us/library/db5x7c0d
and to get the value from the textbox use
string textValue = txtMyValue.Text;
but then use the name you gave to your textbox