Reputation: 177
I need to print the contents of my 2 dimensional array to a textbox called txtExecute in a table like format just like the one below:
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
I have got the format of the weeks and days right but I can't figure out how to get the arrays values exactly like that. Here is my code so far:
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();
}
}
}
And here is the output I am getting:
Mon Tue Wed Thu Fri
Week 0
1111Week 1
1111Week 2
1111Week 3
1111
I realise I am only outputting 1 element, and I will have to print out the rest but I want to solve this problem first.
Upvotes: 0
Views: 1720
Reputation: 2119
So, your ArrayFormat()
function should be like this ...
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";
}
}
EDIT:
txtExecute.Text += "\tMon\tTue\tWed\tThu\tFri\r\n";
is shorter
EDIT:
txtExecute.Text += "Week " + (week+1) + "\t";
to start from Week 1
Upvotes: 3
Reputation: 184
You're always outputting the same array element.
for (int day = 0; day < productsArray.GetLength(0); day++)
{
txtExecute.Text += productsArray[week, day];
}
This will solve that problem.
Upvotes: 1
Reputation: 5890
Replace your tabs with spaces and use String.PadRight
in ArrayFormat method.
And adding array members with productsArray[0, 0]
doesn't help you, either. Replace it with productsArray[week, day];
Upvotes: 0