2016-03-23 31 views
2

チェックリストを電子メールで送信しようとしていますが、行を並べることができません。データベースで文字列を特定の位置に設定します

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) 
    { 
     result += item.ItemTitle.Trim().PadRight(20,' ') + item.Comment.Trim().PadRight(20,' ') + Environment.NewLine ; 
    } 
} 

コードは各欠陥を表示するには、周りにループし、それが別の行にコメントだが、それは次のようになりますので、欠陥やコメントのリストは、そこにある:

Defects:   Comments:      
Vehicle Secure   comment1      
Brakes    comment2 

がありますコメントを整列させる方法?これは、vehicle secureの文字列がコメントを押しているようです。長いリストの欠陥があり、各文字列がどのくらいの長さになるかわからないので、ある位置に表示するようコメントを設定できますか?

+0

'item.ItemTitle'の長さを取得し、それを使用して、2番目の列を必要な方法で整列させます。 –

+1

固定幅フォント(Courier?)を使用する必要があります。異なる文字は異なる容量のスペースを占めるため、物事は整列しません。 –

答えて

1

質問からコードを実装

  var htmlstring = "<table>"; 
      htmlstring += "<tr><th>Header</th><th>Header</th></tr>"; 
      foreach (var row in content) 
      { 
       htmlstring += string.Format("<tr><td>text</td><td>{0}</td></tr>", row.data); 
      } 

      htmlstring += "</table>"; 

      var message = new MailMessage(); 
      message.IsBodyHtml = true; 
      message.Body = htmlString; 

まず、最大パディングを置くために、変数paddingを行い、すべてのあなたのCheckItems通過し、最長ItemTitle

セカンドを見つけます。

最後に、あなたはあなたの現在のフォントの文字のそれぞれが同じ幅を持つべきString.Format

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

int maxlength = 0; 
foreach (var item in chkList.CheckItems) 
{ 
    if (item.ItemTitle.Length > maxlength) 
     maxlength = item.ItemTitle.Length; 
} 

int padding = maxlength + 10; //10 spaces between the longest 'Defects' and its 'Comments' 
string format = "{0,-" + padding + "} {1,-" + padding + "}\r\n" 

string result = String.Format(format, "Defects:", "Comments:"); 

foreach (var item in chkList.CheckItems) 
{ 
    if (item.Defect == true) 
    { 
     result += String.Format(format, item.ItemTitle, Item.Comment); 
    } 
} 

を使用することにより、各行のパディングをカウントすることができます。

あなたのメールには、Monospaced Fontを正しく使用する必要があります。

すべてのフォントで機能したい場合は、代わりに@ Gelootnの答えを見てください。

3

文字列をHTML形式でフォーマットすると、表を使用してフォーマットすることができます。 mailmessageにはその内容をHtmlとして設定するプロパティがあります。あなたは、文字列の長さをカウントする必要があり

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

     string result = string.Format("<table><tr><th>{0}</th><th>{1}</th></tr>", aheading, bheading); 

     foreach (var item in chkList.CheckItems) 
     { 
      if (item.Defect == true) 
      { 
       result += string.Format("<tr><td>{0}</td><td>{1}</td></tr>", item.ItemTitle.Trim(), item.Comment.Trim()); 
      } 
     } 

     result += "</table>"; 

     var message = new MailMessage(); 
     message.IsBodyHtml = true; 
     message.Body = result; 

     // SEND MESSAGE 
     var client = new SmtpClient("mailhost"); 
     // If auth is needed 
     client.Credentials = new NetworkCredential("username", "password"); 
     client.Send(message); 
+0

テーブルを使用すると、フォントの幅に関係なく、メールを簡単かつ簡単にフォーマットできます。 +1のインスピレーション。 – J3soon

+0

例には感謝しますが、私はこれを私のコードにどのように適用するのか分かりません – user123456789

+0

あなたのコードで答えを更新しました。 – Gelootn

1

.padrightではなくString.Formatを使用します。あなたのループの使用でその後

string title = String.Format("{0,-10} {1,-10}\n", "Defects:", "Comments:"); 

は、次のような文字列を作成します

string result = String.Format("{0,-10} {1,-10}\n", item.ItemTitle, item.Comment); 

あなたはそれはあなたが満足しているか見てもらうために値を調整する必要があります。

関連する問題