How to align output text in C# .net - c#

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.

Related

Suppress formatting characters in Excel in C#

I am building an Excel sheet in C# and there is one field that gets converted to scientific notation and I want it to stay text. When I prepend the value with '=""' it displays it like this 20211104075106.399=" ", which is the correct value but I don't want the =" " at the end. This is how I am adding it to the value:
excelWorksheet.Cells[intRow, 5] = string.Join("=\" ", row[4].ToString(), "\"");
Is there any way to suppress that? Thanks.
To force text formatting to a cell, you prepend the value with an apostrophe
excelWorksheet.Cells[intRow, 5] = $"'{row[4]}";

Unable to search for newline character in string

In C#, I'm looking for the index of a string inside a string - specifically the index where the newline (\n) character lives.
Given a string with Windows line breaks (\r\n):
If I look for "\n", it gives me -1. If I look for "\r\n", I get a result. If I look for '\n' as a character, I get a result.
Given a string with Unix line breaks (\n), I get a result.
string s = "hello\r\nworld";
Console.WriteLine(#"\r\n index: " + s.IndexOf("\r\n")); // 5
Console.WriteLine(#"\n index as string: " + s.IndexOf("\n")); // -1
Console.WriteLine(#"\n index as char: " + s.IndexOf('\n')); // 6
s = "hello\nworld";
Console.WriteLine(#"\n index as string: " + s.IndexOf("\n")); // 5
Console.WriteLine(#"\n index as char: " + s.IndexOf('\n')); // 5
I understand that line breaks are two characters, and if I was using StreamReader or File.ReadAllLines or something like that, then it would be handled automatically and I'd lose them.
I thought \n was a valid string by itself, and that \r\n, while special, still represented two separate and distinct characters in a string. But this is telling me otherwise.
I can do IndexOf on the character instead of the string ('\n' instead of "\n"), but I'd really like to know why this is happening so I can plan for it.
EDIT
FYI: Just found that converting the string to a Span gives the correct result. Not sure the overhead involved in that, so I don't know how this compares with the Ordinal solution - I'm guessing the Ordinal is the better one:
Console.WriteLine(#"\n index as string Ordinal: "
+ s.IndexOf("\n", StringComparison.Ordinal)); // 6
Console.WriteLine(#"\n index as Span: "
+ s.AsSpan().IndexOf("\n".AsSpan())); // 6
Console.WriteLine(#"\n index as string with s.AsSpan(): "
+ s.AsSpan().IndexOf("\n")); // 6
There was a change in .Net 5.0 with the globalization libraries for Windows. In previous versions, NLS was used on Windows and ICU on Unix. .Net 5 uses ICU on both to make cross platform development consistent, at the cost of surprising Windows developers (sigh). Due to this change, you must pass StringComparison.Ordinal to find newline in a string.
Note that this can also depend on the version of Windows (double sigh) as Windows 10 May 2019 includes the ICU library and earlier versions that don't will cause .Net 5 to fall back to NLS.
See this article from Microsoft.
This article has more details on the APIs affected.
You may use System.Environment.NewLine in your script, which is a conditional property for the newline character, depending on operating system. Check here.
On Windows: "\r\n".
On unix-platforms: "\n".
using System;
string s = "hello" + Environment.NewLine + "world";

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.

C# - Textbox Newline Problems

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"

Categories

Resources