如何将2维数组的元素打印到格式化的表格中的文本框中?

时间:2014-01-20 08:27:26

标签: c# arrays multidimensional-array

我需要在文本框中的格式表格中打印出二维数组的元素,尺寸为4,5,因此有4行和5列。我知道我必须使用嵌套循环,但我是c#的新手,我只是不知道如何构造循环。

//this is my array 
int[,] productsArray = new int[4, 5];

已经分配了数组的元素。这是我正在寻找的格式。

        Monday  Tuesday  Wednesday  Thursday  Friday
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

为了给你一些更多的上下文,这是我已经分配数组元素的代码。

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

5 个答案:

答案 0 :(得分:0)

您可以在PadLeft的帮助下完成冻结:

public static String FormatArrayOut(int[,] productsArray) {
  StringBuilder Sb = new StringBuilder();

  // You'd probably load week days' names from resource
  String[] weekdayNames = new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satturday", "Sunday"};
  int weeksLength = (productsArray.GetUpperBound(1) + 1).ToString().Length;

  // Head
  Sb.Append(' ', "week ".Length + weeksLength);

  for (int i = 0; i <= productsArray.GetUpperBound(0); ++i) {
    Sb.Append("  ");
    Sb.Append(weekdayNames[i]);
  }

  // Body
  for (int i = 0; i <= productsArray.GetUpperBound(0); ++i) {
    Sb.AppendLine();
    Sb.Append("week ");
    Sb.Append((i + 1).ToString().PadLeft(weeksLength));

    for (int j = 0; j <= productsArray.GetUpperBound(1); ++j) {
      Sb.Append(' ');
      Sb.Append(productsArray[i, j].ToString().PadLeft(weekdayNames[j].Length + 1));
    }
  }

  return Sb.ToString();
}

...

int[,] productsArray = new[,] {
  {1, 2, 3, 4, 5},
  {6, 7, 8, 9, 10},
  {11, 12, 13, 14, 15},
  {16, 17, 18, 19, 20}
 };

String result = FormatArrayOut(productsArray);

结果是

        Monday  Tuesday  Wednesday  Thursday  Friday
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

答案 1 :(得分:0)

您可以按如下方式将数据添加到数组中,

for (int i = 0; i < productsArray.GetLength(0); i++)
        {
            for (int j = 0; j < productsArray.GetLength(1); j++)
            {
                Console.Write("Enter value for " + j.ToString() + " of week " + i.ToString() + " ==>  ");
                string value=Console.ReadLine();
                int intVal=0;
                if (int.TryParse(value, out intVal))
                {
                    productsArray[i, j] = intVal;
                }
                else
                {
                    j--;
                }
            }

如果需要将数组中的所有值打印到文本框,称为txtbx,

 for (int i = 0; i < productsArray.GetLength(0); i++)
        {
            for (int j = 0; j < productsArray.GetLength(1); j++)
            {
                txtbx.Text = txtbx.Text + "\t" + productsArray[i, j].ToString();
            }
            txtbx.Text = txtbx.Text + "\n";
        }

        }

答案 2 :(得分:0)

好的,首先请注意,如果您使用空格格式化文本布局,则需要使用固定间距字体,例如Courier。

您可以使用名为“复合格式”的内容格式化文本的每一行。

基本上,您可以指定项目的格式,如下所示:{1,7}。这意味着:“获取索引1处的参数并将其右对齐格式化为7个字符的宽度”。

这是一个示例控制台应用程序,可根据您的喜好格式化文本:

using System;
using System.Text;

namespace Demo
{
    internal class Program
    {
        private void run()
        {
            int[,] productsArray = new int[4, 5];

            for (int i = 0, k = 1; i < 4; ++i)
                for (int j = 0; j < 5; ++j, ++k)
                    productsArray[i, j] = k;

            Console.WriteLine(format(productsArray));
        }

        private string format(int[,] productsArray)
        {
            var text = new StringBuilder();

            text.Append("        Monday  Tuesday  Wednesday  Thursday  Friday\n");

            for (int i = 0; i < productsArray.GetLength(0); ++i)
            {
                text.AppendFormat
                (
                    "week {0} {1,7} {2,8} {3,10} {4,9} {5,7}\n",
                    i+1,
                    productsArray[i, 0],
                    productsArray[i, 1],
                    productsArray[i, 2],
                    productsArray[i, 3],
                    productsArray[i, 4]
                );
            }

            return text.ToString();
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}

要在您的计划中使用此功能,而不是Console.WriteLine(format(productsArray));,请执行以下操作:

yourTextBox.Text = format(productsArray);

答案 3 :(得分:0)

试试这个

string[] days = new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

        string week = "week ";
        StringBuilder sb = new StringBuilder();
        sb = sb.Append(' ',week.Length+2);
        for(int i=0;i<days.Length;i++)
        {
            sb = sb.Append(days[i].ToString().PadLeft(10));
        }

        sb = sb.Append("\n");
        for (int i = 0; i < productsArray.GetLength(0); i++)
        {
            sb = sb.Append("week " + (i + 1) + " ");
            for (int j = 0; j < productsArray.GetLength(1); j++)
            {
                sb = sb.Append(productsArray[i, j].ToString().PadLeft(10));

            }
            sb = sb.Append("\n");
        }
        Console.WriteLine(sb.ToString());

答案 4 :(得分:0)

或试试这个,

string[] days = new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

        string week = "week ";
        StringBuilder sb = new StringBuilder();
        sb = sb.Append(' ',week.Length+2);
        for(int i=0;i<days.Length;i++)
        {
            sb = sb.Append(days[i].ToString());
            sb = sb.Append(' ',5);
        }

        sb = sb.Append("\n");
        for (int i = 0; i < productsArray.GetLength(0); i++)
        {
            sb = sb.Append("week " + (i + 1) + " ");
            for (int j = 0; j < productsArray.GetLength(1); j++)
            {
                sb = sb.Append(productsArray[i, j].ToString().PadLeft(days[j].Length));
                sb = sb.Append(' ', 5);
            }
            sb = sb.Append("\n");
        }
        Console.WriteLine(sb.ToString());