2

私は C# の初心者コーダーで、現在レポート ジェネレーターを使用しています。テキスト ファイルからデータを読み取り、結果を複数行のテキスト ボックスに表示しています。現在、データのフォーマットに問題があるため、きれいでプロフェッショナルに見えます。区切りのためにスペースを含む文字列を追加していますが、データが不均一に見えます。

下の図に示すように、複数行のテキスト ボックスにデータを表示する方法はありますか? また、フォームの開始時に複数行のテキストボックスにタイトルを入力する方法はありますか?

ここに画像の説明を入力

コード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static string newLine = Environment.NewLine;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

     private void button1_Click(object sender, EventArgs e)
        {
            {

        double grandtotal = 0.0;
        int totalcount = 0;


        string space2 = "          ";
        string space3 = "                    ";

        for( int i = 0; i < 100; i++ ) {
        grandtotal += totalSales[i];
            totalcount += count[i];
         }
        StringBuilder sb = new StringBuilder();

        textBox1.AppendText( "Product Name" + space2 + "Total Sales" + space3 + "Average" + newLine );

        for( int i = 0; i < 100; i++ ) {
            if( productName[i] != null ) {
            if( totalSales[i] != 0 ) {
                textBox1.AppendText( productName[i] + space3 
                + totalSales[i] + space3 
                + ( totalSales[i] / count[i] ).ToString( "#.##" ) + newLine );
                }
         }
    }

        textBox1.AppendText( "Grand Total" + space3 + grandtotal 
        + space3 + ( grandtotal / totalcount ).ToString( "#.##" ) + newLine );



        }
    }



    }
}
4

1 に答える 1

0

String.Format メソッドを使用できます。

http://www.dotnetperls.com/string-formatを案内できるリンクは次のとおりです。

于 2012-09-19T04:35:12.707 に答える