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";
Related
I need help on creating a new line for my RichTextBox which I cant make work when using CheckBox.
It keeps overlapping instead of creating a newline of words.
Tried using the method of rtbdisplay.text = (display+envrionment.newline);
example from my code:
if (rbtnSmall.Checked == true)
{
rtbDisplay.Text = "displaytext".PadRight(20) + "size".PadRight(23) +
qty.ToString().PadRight(20) + StrongDummy;
}
Use the RichTextBox.Text property or the RichtTextBox.AppendText method to append a string with a newline.
myRichTextBox.Text += Environment.NewLine + "My new line.";
// Or
myRichTextBox.AppendText( Environment.NewLine + "My new line." );
You can use c# Environment.NewLine Property as described in http://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx. Or, the "old style" like #"\r\n".
Rgds,
I'm new to C#, and i'm trying to parse a csv file line by line, to search for a string inside each line and do some action.
My problem is that after reading the line, I used Messagebox.Show("Line: " + CurrentLine); just to check if the output is correct, but while the line should show something like C:\Users\test\Desktop\598\Root\test.xls, it just shows up C (the first char).
I used the same code on a console application and the output is fine.
Is there anything wrong with the Messagebox.Show to show the char : ?
Here is my code:
var reader = new StreamReader(File.OpenRead(Filename));
string Search = "test";
string CurrentLine = reader.ReadLine();
MessageBox.Show("Line: " + CurrentLine);
Thanks
I'm trying to replace pipe symbol(|) with new line(\n) in my text(test1.txt) file. But when I'm trying to save it in text(test2.text) file the result is not coming in my test2.txt file but I see the result in my console window. Any one please help on this.
string lines = File.ReadAllText(#"C:\NetProject\Nag Assignments\hi.txt");
//string input = "abcd|efghijk|lmnopqrstuvwxyz";
lines = lines.Replace('|', '\n');
File.WriteAllText(#"C:\NetProject\Nag Assignments\hi2.txt", lines);
Console.WriteLine(lines);
You can try this one:
lines = lines.Replace("|", Environment.NewLine);
It returns "\r\n", for non-Unix platforms according to documentation.
Seems like you want multiple things here. (both original question and subsequent comments)
One is to separate the lines and be able to reference them separately:
string[] separatedLines = lines.Split('|');
The other is to join them back together with a different separator:
string rejoinedLines = string.Join(Environment.NewLine, separatedLines);
You then have access to the individual lines from the separatedLines variable above such as separatedLines[0] and you can also write the rejoinedLines variable back to the other file like you wanted.
EDIT: For example, the following code:
string lines = "a|bc|def";
string[] separatedLines = lines.Split('|');
string rejoinedLines = string.Join(Environment.NewLine, separatedLines);
for (int i = 0; i < separatedLines.Length; i++)
{
Console.WriteLine("Line {0}: {1}", i + 1, separatedLines[i]);
}
Gives output of:
Line 1: a
Line 2: bc
Line 3: def
Instead of:
lines = lines.Replace('|', '\n');
Try:
lines = lines.Replace("|","\r\n");
string[] space = lines.Split ('|');
Will save every substring in space.
The line break should be \r\n for carriage return. It depends if you are reading a file binary or text mode. \n is used in text mode while \r\n is used in binary mode.
How will I get a messagebox "Or another way of doing this" to have multiple lines?
I've got a messageBox to appear when the user presses F1 and I need it to have a sort of list:
Product Name = Alphanumeric + Special Characters.
Quantity = Maximum 100.
Price = Must be Numeric.
Etc
Thanks.
You can use \r\n or Environment.NewLine at the end of each line to be shown or you can use the StringBuilder class:
var message = new StringBuilder();
message.AppendLine("Product Name = Alphanumeric + Special Characters.");
message.AppendLine("Quantity = Maximum 100.");
message.AppendLine("Price = Must be Numeric.");
MessageBox.Show(message.ToString());
Add/concatenate Environment.Newline to your string.
Put "\r\n" at the end of each line of the message.
so, for example:
var message = "Product Name = MyProduct.\r\n";
message += "Quantity = Maximum 100.\r\n";
message += "Price = Must be Numeric.\r\n";
MessageBox.Show(message);
Just use Environment.NewLine where you want a newline to appear. For example:
string message = "Line 1" + Environment.NewLine + "Line 2";
MessageBox.Show(message);
This will output:
Line 1
Line 2
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