Reputation: 163
I am having trouble with text formatting while trying to output my array into a textbox.
I'm required to output something like this; https://i.sstatic.net/sX6AI.jpg sort of like a table minus the borders.
Ive managed to produce something similar, but i'm out of ideas. https://i.sstatic.net/rvAUY.jpg
My code is:
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";
}
Upvotes: 0
Views: 1397
Reputation: 6482
Have you looked into the StringBuilder Class?
Ref- 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.
That should be a good place to start.
Upvotes: 1
Reputation: 2477
Check out my way of doing it. I just read your message so yeah... Maybe this will also help in the future... This is the shortest I could think of :)
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";
}
}
Upvotes: 0
Reputation: 2226
The easiest way to do this is by drawing line by line as follows:
//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";
}
Upvotes: 2
Reputation: 490
just a little idea, you'd better init the titles(like "Mon", "Tue"..etc.. ) into the array, like this
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"}
};
and
toys[week + 1, day + 1] = Microsoft.VisualBasic.Interaction.InputBox(...
and when you output, use the GetLenght() cause toys[,] is a two-dimensional array.
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";
}
result https://i.sstatic.net/LZu7u.jpg
Upvotes: 1