0

I'm trying to display something like this, in the textbox.

Name    Test    Basket  Destructive   Final Grade
--------------------------------------------------
Alex    0       0        0            0
Danny   0       0        0            0
Dave    0       0        0            0

This is what I'm getting:

Name    Test    Basket  Destructive   Final Grade
--------------------------------------------------
Alex       0           0         0            0
Danny      0           0         0            0
Dave       0           0         0            0

It's worse than that because I think the 0's are in the wrong place.

The idea is to have the columns at the top, and underneath it should look something like:

Name - Grade 1 - Grade 2 - Grade 3 - Final Grade

What am I doing wrong?

I'm absolutely awful at for loops too which is why I think the 0's are in the wrong place.

string[] Columns = { "Name","Test", "Basket","Destructive","Final Grade"};
string[] Names = { "Alex", "Danny", "Dave", "Goerge", "Hannan", "Ian", "Muna" };     

int[,] Grade = new int[7, 3];
int[] FinalGrade = new int[7];

private void uiShowMarksAZButton_Click(object sender, EventArgs e)
{
        //test
        Grade[0, 0] = 10;
        //test

        uiMarksDisplayTextBox.Text = string.Join("\t", Columns);
        uiMarksDisplayTextBox.Text += System.Environment.NewLine;

        for (int i = 0; i < Names.Length; i++ )
        {                
            uiMarksDisplayTextBox.Text += (Names[i]);
            for (int x = 0; x < 3; x++)
            {
                uiMarksDisplayTextBox.Text += ("\t" + Grade[x, x]);                  
            }
            uiMarksDisplayTextBox.Text += System.Environment.NewLine;
        }            
    }

EDIT 1: to add some clarification, I am been given the layout of the form by my professor, I am not allowed to change it, or the textbox, meaning I cannot use datagrids or listviews.

When I input Alex's grades, it looks something like this:

Name    Test    Basket  Destructive   Final Grade
-------------------------------------------------
Alex    10      0       0
Danny   10      0       0
Dave    10      0       0
George  10      0       0
Hannan  10      0       0
Ian     10      0       0
Muna    10      0       0

How come all the 0's are in the wrong place?

12
  • 1
    Just use a listview/datagrid and be done with it, however if you dont want to do that, you will have to pad the items appropriately Commented Apr 11, 2019 at 21:31
  • 1
    Have you considered using a DataGridView or a ListView to present that data? Commented Apr 11, 2019 at 21:31
  • Without "winforms" tag this would be duplicate of stackoverflow.com/questions/4579506/… … but sting.Format is very strange choice for WinForms where so many rich table layout controls exist. Commented Apr 11, 2019 at 21:35
  • 1
    Try setting a Monospace font on the TextBox. That should do the trick. Commented Apr 11, 2019 at 21:35
  • Don't add all those spaces. Just the tabs. Use "\t" on your Collumns join. Destructive is too long of a word and will push the next column header over an extra tab. And yes, wrong tool to use for this. Commented Apr 11, 2019 at 21:40

1 Answer 1

0

So first, you can just use tabulator spaces to align everything in columns. Then you need to adjust the indexing, there are some typos and also the final grade is missing. The longest text string is longer than the typical tab space, therefore it would be good to adjust the tab space. This can be done as described in the answer

How to set the TAB width in a Windows Forms TextBox control?

The further code contains everything of this, it also needs the SendMessage function from the answer above

using (Graphics graphics = uiMarksDisplayTextBox.CreateGraphics())
{
    int nMaxLength = (int)Columns.Concat(Names).Max(c => graphics.MeasureString(c + " ", uiMarksDisplayTextBox.Font).Width);
    SendMessage(uiMarksDisplayTextBox.Handle, EM_SETTABSTOPS, 1, new int[] { nMaxLength });
}

uiMarksDisplayTextBox.Text = string.Join("\t", Columns);
uiMarksDisplayTextBox.Text += System.Environment.NewLine;

for (int i = 0; i < Names.Length; i++)
{
    uiMarksDisplayTextBox.Text += Names[i];
    for (int x = 0; x < 3; x++)
    {
        uiMarksDisplayTextBox.Text += "\t" + Grade[i, x];
    }
    uiMarksDisplayTextBox.Text += "\t" + FinalGrade[i];
    uiMarksDisplayTextBox.Text += System.Environment.NewLine;
}
Sign up to request clarification or add additional context in comments.

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.