C# - Textbox Newline Problems - c#

I have a textbox, and I'm trying to print to it with the following line of code:
logfiletextbox.Text = logfiletextbox.Text + "\n\n\n\n\n" + o + " copied to " + folderlabel2.Text;
Where folderlabel 2 is obviously a textbox. The first thing I've put in is the same textbox, so that no text is erased. The excessive new lines have proven my problem, because there are no new lines in the textbox (yes, set to multiline). The "o" is of type FileInfo in a FileInfo array.
Why won't these newlines show up in the text box?

Use "\r\n" instead of "\n". Windows text boxes need CRLF as line terminators, not just LF.
Potentially you could use Environment.NewLine instead - but I don't know what Mono TextBoxes do in terms of working with "\n" (which is what Environment.NewLine would be on a Linux box). If it starts putting extra stuff at the end if you use "\r\n" then that will break plenty of existing apps - but if it requires "\r\n" that would break apps which use Environment.NewLine.
Environment.NewLine is meant to be the default new line for the whole platform you're running on - but what if you're using a widget toolkit which does one thing, but text files typically do something else? Frankly it's a bit of a mess. It would be nice if there were a separate TextBox.NewLine property which different implementations could handle appropriately.

I believe TextBoxes want an Environment.NewLine (which should be "\r\n")
Note that it must be the carriage return (\r) followed by the new line (\n). If you reverse the order, it won't work.

A TextBox control expects a Carriage Return before your Line Feeds (0x0D 0x0A). Use "\r\n" or System.Environment.Newline.

in stead of using \n we can use
Environment.NewLine
i hope it will help

This is how i created append and new line for display
txtitems.Text = txtitems.Text + Environment.NewLine + dr[0].ToString() +" "+dr[1].ToString();

Anyone that is using VB.net, be on the lookout for vbCr
Here is an example:
return "My name is" & vbCr & "John" & vbCr & "Doe"

Related

Adding a NewLine Character to a Textbox in an ActiveX control in C#

I'm trying to add a newline character to a string that I want to print out to a status box in my ActiveX control. The status box is a multiline TextBox. I've tried adding "\r\n" to the string, I've tried add System.Environment.NewLine to the string, but nothing seems to work. Not sure what else to trying. Any ideas would be appreciated.
Environment.NewLine returns a platform-specific string for beginning a new line, which should be:
"\r\n" (\u000D\u000A) for Windows
"\n" (\u000A) for Unix
Or you could try to use StringBuilder with the following:
sb.AppendLine(someText);
sb.AppendLine("");
sb.AppendLine(moreText);

New line character in text file (C#)

I'm exporting text to a file in C# using System.IO.File.AppendAllText, and passing in the text file, and then the text I want to export with \n added to the end. When I view the text document, they are not on different lines, although that pesky return-line character is there between the lines. So the system may think it's two line, but a user sees it as one. How can this be fixed automatically without doing a find-replace every time I generate a file?
System.IO.File.AppendAllText(#"./WarningsLog.txt", line + "\n");
You need to use the Environment.NewLine instead of \n, because newline can be more than that. in windows (if I'm not mistaken), the default is actually \r\n
Although, using \r\n, will help you temporary, using Environment.NewLine is the proper way to go
First off, there are a couple of ways to represent the new line.
The most commonly used are:
The unix way - to write the \n character. \n here represents the newline character.
The windows way - to write the \r\n characters. \r here goes for the carriage return character.
If you are writing something platform-independent, Environment.NewLine will do the job for you and pick the correct character(s).
MSDN states it represents:
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
Also, in some cases you may want to use System.IO.File.AppendAllLines that takes an IEnumerable<string> as the lines collection and appends it to the file. It uses Environment.NewLine inside.
You could try building this with some file specific characters checks , like
new line, tab , etc....
Here is an example code which checks for new line and tabs :
public static string Replace()
{
string rLower = words.ToLower().Replace(Environment.NewLine, "<replaced_newLine>");
rLower = rLower.Replace("\t", "<replaced_Tabulation>");
return rLower;
}
Of course you might have a lot of different combinations , where an item that needs to be changed is followed by " " or "\n" or "\r\n" or "\t"

WatiN and breaklines

I want to enter value into text field with break lines to check some functionality of application. Like this:
value1
value2
I used Environment.NewLine, "\r\n" but WatiN translates them into spaces:
MyField.Value=("value1"+Environment.NewLine+"value2");
MyField.Value=("value1"+"\r\n"+"value2");
Manually all is OK. Is there a way to really imitate entering a break line in WatiN?
Both solutions worked for me in a textarea set to more than 1 row:
textArea.SetAttributeValue("value", "abc" + Environment.NewLine + "123");
textArea.TypeText("abc" + Environment.NewLine + "123");
Make sure you set your Field to support multiple lines.

Line Break using Clipboard SetText

How can I add line breaks using the SetText method?
I tried Clipboard.SetText("eee \n xxxx"); but it doesn't give me the expected result when I paste the clipboard data in the notepad.
Expected result:
eee
xxxx
How can I accomplish this?
Windows uses CR+LF pairs to indicate a new line. This equates to "\r\n" in C#. However, you are just sending "\n", i.e. a single LF rather than a CR+LF pair.
Use Environment.NewLine rather than of "\n". This is the idomatic way to spell "\r\n" in C#. As a bonus, if you ever ran your code on a *nix platform, Environment.NewLine would evaluate to "\n" which is the *nix new line indicator. Finally, in my view Environment.NewLine is preferable from a readability perspective. It documents what this thing is logically rather than relying on you knowing the magic constants.
Try:
Clipboard.SetText("eee" + Environment.NewLine + "xxxx");
I found this works good as well:
System.Windows.Forms.Clipboard.SetText(this.txtCopyJS.Text.Replace("\n", "\r\n"));
I know this is a bit late... but I often use StringBuilder for memory and performance reasons and it' has a very handy AppendLine() method.

C# Reading a file and writing out replacing string

What I have is a C# windows app that reads a bunch of SQL tables and creates a bunch of queries based on the results. What I'm having a small issue with is the final "," on my query
This is what I have
ColumnX,
from
I need to read the entire file, write out exactly what is in the file and just replace the last , before the from with nothing.
I tried .replace(#",\n\nfrom),(#"\n\nfrom) but it's not finding it. Any help is appreciated.
Example:
ColumnX,
from
Result:
ColumnX
from
The line break is most likely the two character combination CR + LF:
.replace(",\r\n\r\nfrom","\r\n\r\nfrom")
If you want the line break for the current system, you can use the Environment.NewLine constant:
.replace(","+Environment.NewLine+Environment.NewLine+"from",Environment.NewLine+Environment.NewLine+"from")
Note that the # in front of a string means that it doesn't use backslash escape sequences, but on the other hand it can contain line breaks, so you could write it in this somewhat confusing way:
str = str.replace(#",
from", #"
from");
There are two solutions that you can try:
Remove the # symbol, as that means it's going to look for the literal characters of \n rather than a newline.
Try .replace("," + Environment.NewLine + Environment.NewLine + from, Environment.NewLine + Environment.NewLine + "from)
Instead of replacing or removing the comma when you read the file, it would probably be preferable to remove it before the file is written. That way you only have to bother with the logic once. As you are building your column list, just remove the last comma after the list is created. Hopefully you are in a position where you have control over that process.
If you can assume you always want to remove the last occurrence of the comma you can use the string function LastIndexOf to find the index for the last comma and use Remove from there.
myString = myString.Remove(myString.LastIndexOf(","), 1);
What about using Regex? Does that handle different forms of linefeed better?
var result = Regex.Replace(input, #",(\n*)from", "$1from");

Categories

Resources