将多维数组输出到网格样式文本框

时间:2014-01-22 03:39:20

标签: c# arrays

我在尝试将数组输出到文本框时遇到文本格式问题。

我需要输出这样的东西; http://imgur.com/jQFYsXA,c0ihHyI 有点像桌子减去边界。

我设法生产类似的东西,但我没有想法。 http://imgur.com/ZwHh7qj

我的代码是:

string[,] toys = new string[5, 4];
for (int week = 0; week <= 3; week++)
{
    for (int day = 0; day <= 4; day++)
    {
        toys[day, week] = Microsoft.VisualBasic.Interaction.InputBox("Please enter value for Day " + Convert.ToString(day + 1) + " in week " + Convert.ToString(week + 1) + ".");
    }
}
txtOutput.Text += "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\t" + "\r\n";
txtOutput.Text += "Week 1" + "\t" + "\r\n";
txtOutput.Text += "Week 2" + "\t" + "\r\n";
txtOutput.Text += "Week 3" + "\t" + "\r\n";
txtOutput.Text += "Week 4" + "\t" + "\r\n";

foreach (string text in toys)
{

    txtOutput.Text += text + "\t";
}

4 个答案:

答案 0 :(得分:2)

最简单的方法是逐行绘制,如下所示:

//first, set up the toys index by accepting some inputs
string[,] toys = new string[5, 4];
for (int week = 0; week <= 3; week++)
{
    for (int day = 0; day <= 4; day++)
    {
        toys[day, week] = Microsoft.VisualBasic.Interaction.InputBox("Please enter value for Day " + Convert.ToString(day + 1) + " in week " + Convert.ToString(week + 1) + ".");
    }
}


//then, print the output line by line by looping through the toys array
//the first line must be separate because the headings are not part of the array
txtOutput.Text += "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\t" + "\r\n";

for (int week = 0; week <= 3; week++)//foreach week
{
    //construct the line of text which represents the week's data
    txtOutput.Text += "\tWeek " + (week+1) + "\t";
    for (int day = 0; day <= 4; day++)
    {
       txtOutput.Text += toys[day,week];
       if(day != 4)
       {
         //so long as it is not the last day, then you have to tab over
         txtOutput.Text += "\t";
       }
    }

    //wrap things up by moving to the next line before you iterate to the next line
    txtOutput.Text += "\r\n";
 }

答案 1 :(得分:2)

您是否查看了StringBuilder类?

参考 - http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx

StringBuilder myName = new StringBuilder(); 
myName.appendFormat("Name = {0}, hours = {1:hh}", myName, DateTime.Now); //For example.

这应该是一个好的开始。

答案 2 :(得分:1)

只是一个小小的想法, 你最好把标题(比如“Mon”,“Tue”等等)添加到数组中,就像这样

string[,] toys = new string[,]
{
    {" ","Mon", "Tue", "Wed", "Thu", "Fri"},
    {"Week 1", "0", "0", "0", "0", "0"},
    {"Week 2", "0", "0", "0", "0", "0"},
    {"Week 3", "0", "0", "0", "0", "0"},
    {"Week 4", "0", "0", "0", "0", "0"}
};

toys[week + 1, day + 1] = Microsoft.VisualBasic.Interaction.InputBox(...

当你输出时,使用 GetLenght()因为玩具[,]是一个二维数组。

for (int i = 0; i < toys.GetLength(0); i++)
{
    for (int j = 0; j < toys.GetLength(1); j++)
    {
        this.textBox1.Text += toys[i, j] + "\t";
    }
    this.textBox1.Text += "\r\n";
}

结果 http://i.stack.imgur.com/LZu7u.jpg

答案 3 :(得分:0)

看看我这样做的方式。我只是读了你的信息所以是的...也许这对未来也有帮助......这是我能想到的最短的:)

TextBox1.Text = "\t" + "Mon" + "\t" + "Tues" + "\t" + "Weds" + "\t" + "Thurs" + "\t" + "Fri";
for (int week = 0; week <= 3; week++)
    {
    TextBox1.Text += Environment.NewLine + "Week " + Convert.ToString(week + 1) + "\t";
    for (int day = 0; day <= 4; day++)
        {
        TextBox1.Text += toys[day, week] + "\t";
        }
    }