【问题标题】:how to print the contents of a 2 dimensional array to a text box in a table like format?如何将二维数组的内容打印到表格格式的文本框中?
【发布时间】:2014-01-23 08:53:07
【问题描述】:

我需要将二维数组的内容打印到一个名为 txtExecute 的文本框,格式如下:

        Mon    Tue    Wed    Thu    Fri
Week 1  1      2      3      4      5
Week 2  6      7      8      9      10
Week 3  11     12     13     14     15
Week 4  16     17     18     19     20

我得到了正确的星期和日期的格式,但我不知道如何得到完全一样的数组值。到目前为止,这是我的代码:

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

    int[,] productsArray = new int[4, 5];

    private void AddValuesToArray() 
    {
        txtExecute.Text = "Filling the array with user input..." + "\r\n\r\n";

        String value;
        int num;

        for (int week = 0; week < productsArray.GetLength(0); week++)
        {
            for (int day = 0; day < productsArray.GetLength(1); day++)
            {
                value = Microsoft.VisualBasic.Interaction.InputBox("Enter value for " + day + " of week " + week, "Enter Value");
                try
                {
                    while (!(int.TryParse(value, out num)))
                    {
                        MessageBox.Show("Not a valid number, try again.");
                        value = Microsoft.VisualBasic.Interaction.InputBox("Enter a number", "Enter Number");

                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Value entered is not in a valid format");
                }

                productsArray[week, day] += int.Parse(value);
            }               
        }
        txtExecute.Text += "The product allocation is as follows:" + "\r\n\r\n";
    }

    private void ArrayFormat()
    {
        txtExecute.Text += "\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\r\n";
         for(int week = 0; week < productsArray.GetLength(0); week++)
         {
             txtExecute.Text += "Week " + week + "\t" +"\r\n";
             for (int day = 0; day < productsArray.GetLength(0); day++)
             {
                 txtExecute.Text += productsArray[0, 0];
             }
         }

    }

    private void btnExecute_Click(object sender, EventArgs e)
    {
        AddValuesToArray();
        ArrayFormat();
    }
 }

}

这是我得到的输出:

        Mon    Tue    Wed    Thu    Fri
Week 0  
1111Week 1  
1111Week 2  
1111Week 3  
1111

我意识到我只输出了 1 个元素,我将不得不打印出其余的元素,但我想先解决这个问题。

【问题讨论】:

  • 我强烈建议为此使用DataGridView。否则,最好的办法是使用等宽字体并使用空格来排列。 (如果您尝试使用标签,您将陷入痛苦的世界......)

标签: c# arrays multidimensional-array output


【解决方案1】:

所以,你的ArrayFormat() 函数应该是这样的......

private void ArrayFormat()
{
    txtExecute.Text += "\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\r\n";
     for(int week = 0; week < productsArray.GetLength(0); week++)
     {
         txtExecute.Text += "Week " + week + "\t";
         for (int day = 0; day < productsArray.GetLength(0); day++)
         {
             txtExecute.Text += productsArray[week, day] + "\t";
         }
         txtExecute.Text += "\r\n";
     }
}

编辑:

txtExecute.Text += "\tMon\tTue\tWed\tThu\tFri\r\n";

更短

编辑:

txtExecute.Text += "Week " + (week+1) + "\t";

Week 1开始

【讨论】:

  • 我敢打赌,当数字变大时,标签不会正确排列。
  • @MatthewWatson 啊,是的,当然,我同意你的看法,但是 OP 只希望打印小于 32 的日期值......并且 OP 可能仍需要跳过 2 天的值SunSat 和第 1 天可能从 TueWed 开始 :)
  • 很公平 - 它可能有效!尝试使用具有比例字体的标签来排列内容时,我总是遇到无穷无尽的麻烦。
  • 是的,这个数字不会超过 20,所以我不担心大数字。这几乎可以完美地工作,但由于某种原因,星期五栏是空白的,你知道这是为什么吗?我也知道这不是问题的一部分,但你知道我怎么能说第 1 周 2 周 3 周 4 而不是第 0 周 第 1 周 2 第 3 周?
  • 将此行 txtExecute.Text += "Week " + week + "\t"; 更改为 txtExecute.Text += "Week " + (week+1) + "\t";,您将投注第 1 周 2 周 3 周 4
【解决方案2】:

你总是输出相同的数组元素。

for (int day = 0; day < productsArray.GetLength(0); day++)
{
     txtExecute.Text += productsArray[week, day];
}

这将解决这个问题。

【讨论】:

    【解决方案3】:

    用空格替换你的制表符并在 ArrayFormat 方法中使用String.PadRight。 并且使用productsArray[0, 0] 添加数组成员也对您没有帮助。将其替换为productsArray[week, day];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2018-06-07
      相关资源
      最近更新 更多