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.
Related
I have some datarows and I want to display them in a textbox;
This is some part of my code:
strDetails += "Challenge ID" + "\t" + "Challenge Name" + "\t\t" + "Start Time" + "\r\n\r\n";
strDetails += drChallenge["ChallengeID"] + "\t\t" + drChallenge["ChallengeName"] + "\t\t" +
startTime.ToString("h:mm tt") + "\r\n";
However, the output has some issues with the time string. I dont know how to align them.
You can iterate a member of a columns, get a max lengh a column and take a padleft(max_lengh) all members at column to a max lengh.
The line where the time is out of band, has a Challenge Name of less than 8 characters. A tab spans (by default) 4 spaces. If the Challenge Name is more than 12 characters, the time field would go out of band to the other side.
The real solution is to use something intended for complex formatting. For example you could output a CSV file that can be opened by Excel, or use a layout language such as HTML.
If you really want to do this with text in a textbox then you can:
ensure Challenge Name is no longer than 11 characters
render the Challenge Name as drChallenge["ChallengeName"].PadRight(8, ' ') to ensure it's always at least 8 characters, filled with spaces.
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);
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"
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"
I have written code in C# which is exceeding page width, so I want it to be broken into next line according to my formatting. I tried to search a lot to get that character for line break but was not able to find out.
In VB.NET I use '_' for line break, same way what is used in C#?
I am trying to break a string.
In C# there's no 'new line' character like there is in VB.NET. The end of a logical 'line' of code is denoted by a ';'. If you wish to break the line of code over multiple lines, just hit the carriage return (or if you want to programmatically add it (for programmatically generated code) insert 'Environment.NewLine' or '\r\n'.
Edit: In response to your comment: If you wish to break a string over multiple lines (i.e. programmatically), you should insert the Environment.NewLine character. This will take the environment into account in order to create the line ending. For instance, many environments, including Unix/Linux only use a NewLine character (\n), but Windows uses both carriage return and line feed (\r\n). So to break a string you would use:
string output = "Hello this is my string\r\nthat I want broken over multiple lines."
Of course, this would only be good for Windows, so before I get flamed for incorrect practice you should actually do this:
string output = string.Format("Hello this is my string{0}that I want broken over multiple lines.", Environment.NewLine);
Or if you want to break over multiple lines in your IDE, you would do:
string output = "My string"
+ "is split over"
+ "multiple lines";
Option A: concatenate several string literal into one:
string myText = "Looking up into the night sky is looking into infinity" +
" - distance is incomprehensible and therefore meaningless.";
Option B: use a single multiline string literal:
string myText = #"Looking up into the night sky is looking into infinity
- distance is incomprehensible and therefore meaningless.";
With option B, the newline character(s) will be part of the string saved into variable myText. This might, or might not, be what you want.
result = "Minimum MarketData"+ Environment.NewLine
+ "Refresh interval is 1";
Use # symbol before starting the string.
like
string s = #"this is a really
long string
and this is
the rest of it";
If I am understanding this correctly, you should be able to break the string into substrings to accomplish this.
i.e.:
string s = "this is a really long string" +
"and this is the rest of it";
C# doesn't have an explicit line break character. You statements end with a semicolon so you can span your statements over many lines. These are both the same:
public string GenerateString()
{
return "abc" + "def";
}
public string GenerateString()
{
return
"abc" +
"def";
}
All you need to do is add \n or to write on files go \r\n.
Examples:
say you wanted to write duck(line break) cow this is how you would do it
Console.WriteLine("duck\n cow");
Edit: I think I didn't understand the question. You can use
#"duck
cow".Replace("\r\n", "")
as a linebreak in code, that produces \r\n which is used Windows.
C# code can be split between lines on pretty much any syntatic construct without a need for a '_' style construct.
For example
foo.
Bar(
42
, "again");
dt = abj.getDataTable(
"select bookrecord.userid,usermaster.userName, "
+" book.bookname,bookrecord.fromdate, "
+" bookrecord.todate,bookrecord.bookstatus "
+" from book,bookrecord,usermaster "
+" where bookrecord.bookid='"+ bookId +"' "
+" and usermaster.userId=bookrecord.userid "
+" and book.bookid='"+ bookId +"'");
guys.. use resources for long strings in code behind!!
also.. you don't need an _ for codeline breaks in C#. In VB the codelines end with a newline character (or a ':'), using the the _ would tell the parser it has not reached the end of the line yet. The codeline in C# ends with a ';' so you can use newlines to styleformat your code.
Strings are immutable, so using
public string GenerateString()
{
return
"abc" +
"def";
}
will slow you performance - each of those values is a string literal which must be concatenated at runtime - bad news if you reuse the method/property/whatever alot...
Store your string literals in resources is a good idea...
public string GenerateString()
{
return Resources.MyString;
}
That way it is localisable and the code is tidy (although performance is pretty terrible).