0

I have some vehicle information that I want to send in an email. I have all code working but spacing out the information is a problem. Each vehicle has a checklist and that checklist then gets emailed. So I loop through the list and get the defect and the comment.

foreach (var item in chkList.CheckItems)
        {
            if (item.Defect == true)
            {
                defect += string.Format("{0,-40} {1}\n", item.ItemTitle, item.Comment);
            }
        }

        if (hasDefect == true)
        {
            Utils.ChecklistSendMail("Checklist", ToAddresses.Split(';'),
                "Vehicle Reg: " + reg + "\n" +
                "Checklist No: " + chkList.CheckListNo + "\n"+
                "Date: " + ChecklistDate.ToShortDateString() + "\n" +
                "Defects:                            Comments: " + "\n" + 
                defect);
        }

Email then looks like this:

Vehicle Reg: XLZ 8194
Checklist No: 0
Date: 22/03/2016
Defects:                   Comments: 
Vehicle Secure                 comment1
Brakes                     comment2

I want the defects and the comments to be displayed like this:

Defects:                     Comments: 
Vehicle Secure               comment1
Brakes                       comment2

Since Vehicle Secure is longer than Brakes the comment is being pushed further out. But is there a way to fix the string at a certain position no matter how long first word is?

EDIT

code now looks like this:

    string defect = "";
        string comment = "";
        string aheading = "Defects:";
        string bheading = "Comments:";
foreach (var item in chkList.CheckItems)
        {
            if (item.Defect == true)
            {
                defect += item.ItemTitle;
                comment += item.Comment;
            }
        }

        string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine +
           defect.PadRight(20, ' ') + comment.PadRight(20, ' ') + Environment.NewLine;

But the output looks like this:

Defects:            Comments:            
Vehicle SecureBrakestest1test2 
user123456789
  • 1,914
  • 7
  • 44
  • 100

2 Answers2

3

If you really want to do this with spaces, you need to determine the label with the most characters and how much space you want after that. This creates a total number of characters. From this, you subtract the number of characters of the label to get the number of spaces necessary to line up the value.

However, you could just use a <table> or some other html.

A potential, quick and dirty solution would require you to generate the html as a part of your code. I seriously advise against some homegrown html generator logic. Invariably the data involved in the email becomes more complex. This leads to mixing code that is getting the data for the template and building the html, which is painful to debug. Also there are plenty of html templating solutions out there. You'd really be just reinventing the wheel to take on technical debt and the maintenance of more code.

A better solution would be use something like MvcMailer and build an html template. You then pass the template and a context object to the engine to render the resultant html.

Josh C.
  • 4,303
  • 5
  • 30
  • 51
  • is there a way to wrap the string in html or something like that? – user123456789 Mar 22 '16 at 16:09
  • I think I understand what you're asking. I've updated my answer. – Josh C. Mar 22 '16 at 16:23
  • ok thanks for the answer. If I use a `` how would I do that? Could you give an example of how I would use html for the code in my question
    – user123456789 Mar 22 '16 at 16:35
  • Your question is how to place text in an html table element in general? Maybe I don't know what you are asking. Here's an example of how to put text into a table http://www.w3schools.com/html/html_tables.asp – Josh C. Mar 22 '16 at 17:06
  • yes I understand the basics of html table but I am not sure how to that in the code behind. How to I put my code into a table then put that table into the email function? – user123456789 Mar 22 '16 at 17:23
  • Did you take a look at MvcMailer? It uses Razor views, which makes this pretty easy. Do you really want to write code that builds the html? – Josh C. Mar 22 '16 at 17:54
1

Try and use String padding with ' ' as char

public string PadRight(
int totalWidth,
char paddingChar)

This method would complete the length of the string with the chosen char. by specifying the max length of the string and replacing the remaining length with " " (space). you can always have the strings aligned.

Read more about PadRight or PadLeft

string Defects ="Example" Defects.PadRight(20," ");

Result: "Example "

Edit : Example Code .Please have a look at this code and check what you are doing wrong

        string aheading = "Defects:";
        string bheading ="Comments:";
        string b = "Vehicle Secure";
        string bComment = "My Comment value";
        string c = "Brakes";
        string cComment = "My Comment value";


   string result= aheading.Trim().PadRight(20,' ')+bheading.Trim().PadRight(20,' ')+    Environment.NewLine +
       b.Trim().PadRight(20, ' ') + bComment.Trim().PadRight(20, ' ') + Environment.NewLine + 
       c.Trim().PadRight(20,' ')+cComment.Trim().PadRight(20,' ')+Environment.NewLine  ;

        Console.WriteLine(result);

Edit:Answer based on the code you Posted

        string aheading = "Defects:";
        string bheading = "Comments:";


        string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine ;


        foreach (var item in chkList.CheckItems)
        {
            if (item.Defect == true)
            {
                string result += item.ItemTitle.Trim().PadRight(20,' ') +  item.ItemTitle.Trim().PadRight(20,' ') + Environment.NewLine ; 

            }
        }

        Console.WriteLine(result);
Shadi Zidan
  • 154
  • 5
  • I added `defect.PadRight(40, ' ');` to my code but it didn't move the line over – user123456789 Mar 22 '16 at 17:30
  • Hi, Try and replace the ' ' with '* ' just for the sake of testing and let me know what you get – Shadi Zidan Mar 23 '16 at 09:05
  • adding the `*` causes the error: Too many characters in character literal – user123456789 Mar 23 '16 at 09:24
  • you seem to have more than one char between 'char' . use like this '*' – Shadi Zidan Mar 23 '16 at 09:38
  • yes I add a space next to *. That got rid of the error. I edited my question so you can see what I was trying to do. The comments are still not lining up. I found some examples about padright if you look at example 2 from here this is what I am trying to do: http://www.dotnetperls.com/padright – user123456789 Mar 23 '16 at 09:50
  • Please have a look at my edited Answer . Sample Code . i have tried it and tested it works fine. One more thing try and .Trim your strings they might have extra spaces ex defect.Trim().PadRight(40,' '); – Shadi Zidan Mar 23 '16 at 10:08
  • thanks for the sample. I updated my question. The defects and comments are appearing on the same line. Brake and test 2 should be on the second line. I know in your sample code you just hard coded the strings in, but in my code it loops through all the values from the database. I could have more than 2 rows so I can't hard code the strings – user123456789 Mar 23 '16 at 10:28
  • thanks for all your help. It does look a lot better now but the comments still aren't lining up. The first comment is still pushed over further – user123456789 Mar 23 '16 at 11:11
  • Glad it was of help. can you post the code you are running now along with the result; – Shadi Zidan Mar 23 '16 at 11:53