0

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";
}
5
  • Is this Silverlight, or Web Forms? Neither one has a textbox that works like this. Commented Jan 22, 2014 at 3:45
  • It's not the only wrong thing here but, why "Mon" + "\t" instead of "Mon \t" ? Commented Jan 22, 2014 at 3:50
  • Oh i was taught to use the visual basic inputbox (for the current project im working on) so had to use that. Also was unaware i could code it that way "Mon \t", seems alot easier! Commented Jan 22, 2014 at 3:59
  • You still haven't said what kind of project this is. Visual Basic doesn't have any "inputbox", BTW. Commented Jan 22, 2014 at 4:07
  • Oh its windows form & im coding in c#, sorry i forgot to mention that it was a windows form application Commented Jan 22, 2014 at 4:17

4 Answers 4

2

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";
 }
Sign up to request clarification or add additional context in comments.

5 Comments

I like this approach. Seems logical. Although its saying this part is not working. txtOutput.Text += toys[week, day];
Nevermind... I think I switched up your indexes on the toys array. See the edited code above, which should have fixed the issue.
Oh i see what you mean, its still saying "Index was outside the bounds of the array." for the: txtOutput.Text += toys[week,day];
that line of code should now read txtOutput.Text += toys[day,week]; it should not read txtOutput.Text += toys[week,day];
thanks for explaining it! Makes alot more sense now :)
1

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

2 Comments

This way seems like alot more code? For this part: toys[week + 1, day + 1] = Microsoft.VisualBasic.Interaction.InputBox(... Would i have to paste that like for each day of the week? or loop it?
toys[day + 1, week + 1] = Microsoft.VisualBasic.Interaction.InputBox("Please enter value for Day " + Convert.ToString(day + 1) + " in week " + Convert.ToString(week + 1) + ".");
1

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.

Comments

0

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";
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.