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

C# 将多维数组输出到网格样式文本框,c#,arrays,C#,Arrays,我在尝试将数组输出到文本框时遇到文本格式问题 我需要输出这样的东西; 有点像一张没有边框的桌子 我也试着做了一些类似的东西,但我没有主意了。 我的代码是: string[,] toys = new string[5, 4]; for (int week = 0; week <= 3; week++) { for (int day = 0; day <= 4; day++) { toys[day, week] = Microsoft.VisualBas

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

我需要输出这样的东西; 有点像一张没有边框的桌子

我也试着做了一些类似的东西,但我没有主意了。

我的代码是:

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";
}
string[,]toys=新字符串[5,4];

对于(int week=0;week而言,最简单的方法是逐行绘制,如下所示:

//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";
 }
//首先,通过接受一些输入来设置玩具索引
字符串[,]玩具=新字符串[5,4];

对于(int week=0;week而言,最简单的方法是逐行绘制,如下所示:

//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";
 }
//首先,通过接受一些输入来设置玩具索引
字符串[,]玩具=新字符串[5,4];

对于(int week=0;week您研究过StringBuilder类吗

参考-


这应该是一个很好的起点。

您研究过StringBuilder类吗

参考-

那应该是一个很好的起点。

只是一个小主意, 您最好将标题(如“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"}
};

输出时,请使用getLength()因为玩具[,]是二维数组

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";
}
for(int i=0;i
结果 只是一个小主意, 您最好将标题(如“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"}
};

输出时,请使用getLength()因为玩具[,]是二维数组

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";
}
for(int i=0;i
结果

看看我的方法。我刚刚读了你的留言,所以是的……也许这在将来也会有帮助……这是我能想到的最短的:)

TextBox1.Text=“\t”+“周一”+“\t”+“周二”+“\t”+“周三”+“\t”+“周四”+“\t”+“周五”;

对于(int week=0;week查看我的方式。我刚刚读了你的消息,所以是的……也许这在将来也会有帮助……这是我能想到的最短的:)

TextBox1.Text=“\t”+“周一”+“\t”+“周二”+“\t”+“周三”+“\t”+“周四”+“\t”+“周五”;


对于(int week=0;week是Silverlight还是Web表单?两者都没有这样的文本框。这不是这里唯一的错误,但是,为什么
“Mon”+“\t”
而不是
“Mon\t”
?哦,我被教导使用visual basic输入框(对于我正在处理的当前项目)所以我不得不使用它。我也不知道我可以用“Mon\t”这样的方式编写它,这似乎容易多了!你还没有说这是什么类型的项目。Visual Basic没有任何“inputbox”,顺便说一句。噢,它的windows窗体&im用c#编码,对不起,我忘了提到它是windows窗体应用程序是Silverlight,还是Web窗体?没有一个文本框是这样工作的。这不是这里唯一的错误,但是,为什么
“Mon”+“\t”
而不是
“Mon\t”
?噢,我被教使用visual basic inputbox(对于我正在处理的当前项目)所以我不得不使用它。我也不知道我可以用“Mon\t”的方式编写它,这似乎要容易得多!您还没有说这是什么类型的项目。Visual Basic没有任何“inputbox”,顺便说一句。哦,它的windows窗体&im用c#编码,对不起,我忘了提到它是一个windows窗体应用程序,就像这种方法一样。似乎是合乎逻辑的。虽然它说这部分不起作用。txtOutput.Text+=玩具[周,日];没关系……我想我把你在玩具数组上的索引调高了。请看上面编辑的代码,它应该已经解决了这个问题。哦,我明白你的意思了,它仍然说“索引超出了数组的界限”。对于:txtOutput.Text+=toys[week,day];这行代码现在应该是txtOutput.Text+=toys[day,week];它不应该读txtOutput.Text+=toys[week,day];谢谢你的解释!现在更有意义了:)我喜欢这种方法。似乎合乎逻辑。虽然它说这部分不起作用。txtOutput.Text+=toys[week,day];没关系……我想我把你在玩具数组上的索引调高了。请看上面编辑的代码,它应该已经解决了这个问题。哦,我明白你的意思了,它仍然说“索引超出了数组的界限”。对于:txtOutput.Text+=toys[week,day];这行代码现在应该是txtOutput.Text+=toys[day,week];它不应该读txtOutput.Text+=toys[week,day];谢谢你的解释!现在有了更多的意义:)这种方式似乎有更多的代码?对于这部分:toys[week+1,day+1]=Microsoft.VisualBasic.Interaction.InputBox(…我需要为一周的每一天粘贴类似的内容吗?还是循环它?toys[day+1,week+1]=Microsoft.VisualBasic.Interaction.InputBox(“请输入日期值”+Convert.ToString(Day+1)+“周内”+Convert.ToString(week+1)+”);这种方式似乎有更多的代码?对于这部分:玩具[week+1,Day+1]=Microsoft.VisualBasic.Interaction.InputBox(…我需要为一周中的每一天粘贴类似的内容吗?还是循环它?玩具[day+1,week+1]=Microsoft.VisualBasic.Interaction.InputBox(“请输入day”+Convert.ToString(day+1)+“in week”+Convert.ToString(week+1)+”的值);