【问题标题】:Formatting displayed data in Multiline Textbox格式化多行文本框中显示的数据
【发布时间】:2025-11-24 13:50:01
【问题描述】:

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



        }
    }



    }
}

【问题讨论】:

  • 这是windows应用还是asp.net应用?
  • @RajatSaini windows 应用程序
  • 你最好使用VS Reports、Crystal Reports之类的报表工具或者使用html报表并在浏览器控件中显示
  • 是否需要在多行文本框中显示结果?

标签: c# winforms


【解决方案1】:

你可以使用 String.Format 方法。

这是可以指导您的链接http://www.dotnetperls.com/string-format

【讨论】:

    最近更新 更多