I'm using a Xceed.Wpf.Toolkit.RichTextBox that displays text saved in RTF. I've got a contextual menu that add some multi lines text at the caret position using this code
var text = "Line1" + Environment.NewLine + "Line2"
richTextBox.CaretPosition.InsertTextInRun(text);
It displays as I expect
Line1
Line2
When I save and reload the text (saved in RTF), it displays as this:
Line1Line2
When I look in the RTF code, it is saved without the CR and LF...
Why my CR/LF vanished? What is the solution to insert multi line text at the caret position?
I found how to do:
var text = "Line1" + Environment.NewLine + "Line2"
richTextBox.Control.Selection.Text = text;
Set RichTextBox.AcceptsReturn to True
http://social.msdn.microsoft.com/Forums/vstudio/en-US/f044f15b-48a4-485c-91c0-07a0828acb98/acceptsreturn-in-a-wpf-richtextbox
Related
I've got a problem whereby I've created an application where the user enters text into various text boxes. They then click a button which outputs it all into a text log file in a specific format.
I create a string which I output to a file and that string is compiled from various pieces of text and the contents of various form elements.
When it outputs, each separate line which has been created as part of the string creation outputs with CR LF (\r\n) which is how I want it, but any text which was entered into a Rich Text Box outputs with only LF (\n)
Code goes like:
string[] lines = {
#"HEADER TEXT HERE",
#"-----------------------------------------------",
Text_Box.Text,
Rich_Text_Box.Text,
........
Directory.CreateDirectory(#"\\basedirectory\" + project_name_tb.Text + #"\" + strDate);
System.IO.File.WriteAllLines(#"\\basedirectory\" + project_name_tb.Text + #"\" + strDate
+ #"\" + strDate + #"_" + project_name_tb.Text + #"_"
+ session_number_mtb.Text + #".txt", lines);
The rich text boxes are multiline.
How do I make the rich text box output CR LF?
You could try replacing all instances of LF with CRLF:
Rich_Text_Box.Text.Replace("\n", "\r\n")
I think this will solve your problem even it's safe if it's already \r\n it won't replace it.
public string ReplaceRichTextBoxContent(string data)
{
return Regex.Replace(data, "(?<!\r)\n", "\r\n");
}
use it like
string[] lines = {
#"HEADER TEXT HERE",
#"-----------------------------------------------",
Text_Box.Text,
ReplaceRichTextBoxContent(Rich_Text_Box.Text),
I am new to Programming, i am making C# Windows Form Application were, on selecting Tree node, it append the text in Richtextbox:
Qs1: For me Caret is not displayed after selecting the tree Node.
Qs2: Make display like editor, where if word start with // ( Comment) should be in green color.
Thanks
if (treeView1.SelectedNode.Name == "Node1")
{
this.richTextBox1.SelectedText += " my text for Node1" + Environment.NewLine
richTextBox1.Focus();
}
else if (treeView1.SelectedNode.Name == "Node2")
{
this.richTextBox1.SelectedText += " my text for Node2" + Environment.NewLine
richTextBox1.Focus();
}
You're asking two questions related to RichTextBox. The preferred form on StackOverflow is one question per question. You'll probably get more responses with more focused questions.
That being said:
According to the documentation for the Select method:
The text box must have focus in order for the caret to be moved.
So you need to do that first.
In addition, as a general rule, you should never modify the pre-existing Text or SelectedText with += because this will clear away any and all RTF formatting on that text. Instead, to insert text, you should set the selection to the desired location, with length zero, and insert there. Thus:
public static void FocusAndAppendToSelectedText(this RichTextBox richTextBox, string text)
{
Action append = () =>
{
richTextBox.Focus();
var start = richTextBox.SelectionStart;
var length = richTextBox.SelectionLength;
var insertAt = start + length;
richTextBox.Select(insertAt, 0);
richTextBox.SelectedText = text;
};
if (richTextBox.InvokeRequired)
richTextBox.BeginInvoke(append);
else
append();
}
Also, you should use \n rather than Environment.Newline because the latter will get simplified into the former anyway.
A question like "[How to] Make display like editor, where if word start with // ( Comment) should be in green color" is very general. Try to break it down into discrete issues and ask questions for those you can't figure out yourself. To get you started, see this question here: highlight the '#' until line end in richtextbox. However, you may want to set the SelectionBackColor not the SelectionColor, depending on your precise UI requirements.
In my application i have to highlight some text (incorrect words) which are returned by an API to show in a multiline textbox.
How to highlight specific words in the string returned by an API.
My code is
ServiceReference1.GetTextSoapClient c = new GetTextSoapClient();
string text = c.GetTextFromImage(#"D:\Files\OCR\" + FileUpload1.FileName);
txtContent.Text = text;
List<string> list_Words = GetWords(text);
How to highlight specific words in text.
if you just want to select a word in a multiline text box you must first find the word :
int start_index = textBox1.Text.IndexOf("word");
then highlight it :
textBox1.Focus();
textBox1.Select(start_index, word lenght);
but if you want to select multiple words and change the color , highlight background etc.... you must use rich text box
I want to show 2 text file on one richtextbox.for example a.txt(text direction : right to left)and b.txt(text direction :left to right)
I use these codes:
richTextBox1.LoadFile(Application.StartupPath + "\\database\\a.txt");
string x = richTextBox1.Text;
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.LoadFile(Application.StartupPath + "\\database\\b.txt");
richTextBox1.AppendText(x);
richTextBox1.AppendText(System.Environment.NewLine);
richTextBox1.Focus();enter code here
but when run these codes,all lines direction in richtextbox are left to right.how can solve this problem?
You should work with RTF property of the RichTextBox.
First load the content of files a.txt and b.txt to YourFirstTextFileString and YourSecondTextFileString.
Second your code should be:
string rtfsource="{\rtf1\fbidis\ansi\ansicpg1256\deff0\deflang1025{\fonttbl{\f0\fnil\fcharset0 Calibri;}}";
rtfsource = rtfsource + "\\ltrpar "+YourFirstTextFileString
rtfsource = rtfsource + "\\par\\rtlpar "+YourSecondTextFileString +"\\par"
rtfsource = rtfsource + "}"
// Set the rtf format to the richtextbox.
richTextBox1.RTF = rtfsource;
Content of the YourFirstTextFileString will appear Left-To-Right and YourFirstTextFileString will appear Right-To-Left in the RichTextBox.
Hi i am doing a small project in C#, and i need to know what commands are comming from input source so i can do my work accordingly..
here is example...
textBox1.Text = "First line \nSecond line";
richTextBox1.Text = "First line \nSecond line";
Richtextbox shows this output:
First line
Second line
Textbox show this output:
First line Second line
please tell me how to show new line "\n" or return "\r" or similar input as a character output in text or richtextbox. so i can know that newline command is coming from input data.
for example text or richtext box will show this output.
First line \nSecond line
thankx in advance.
Lets say that i have a string which have new line :
string st = "my name is DK" + Environment.NewLine + "Also that means it's my name";
Now that i want to show that there is new line in my text there you go :
textBox1.Text = st.Replace(Environment.NewLine, "%");
This will show the newline chat with % sign
For winforms application set
this.textBox1.Multiline = true;
and use "\r\n" as
textBox1.Text = "First line \r\nSecond line";
You want to either prefix your string with # or you can use a double slash before each n (\n). Both of these are ways of escaping the \ so that it displays instead of being treated as part of a new line.
#"This will show verbatim\n";
"This will show verbatim\\n";
You can utilize this by performing a Replace on your incoming text
richTextBox1.Text = richTextBox1.Text.Replace("\n", "\n\\n");
richTextBox1.Text = richTextBox1.Text.Replace("\r\n", "\r\n\\n");
In the replace, I left the original linebreak so that it will be there, just followed by the displaying version. You can take those out if you dont want that. :)
Use the combination "\r\n" (or Environment.NewLine constant which contains just that).
Change Your text box property from single line to Multiline then it will change to new line
TextBox1.TextMode = TextBoxMode.MultiLine;
TextBox1.Text = "First line \n New line ";
TextBoxt1.Text ="FirstLine \r\n SecondLine";