Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to align output text in C# .net

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

output

enter image description here

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading