我是 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 );
}
}
}
}