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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gentian Gashi
  • 331
  • 2
  • 13
  • 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 – TheGeneral Apr 11 '19 at 21:31
  • 1
    Have you considered using a DataGridView or a ListView to present that data? – Jimi Apr 11 '19 at 21:31
  • Without "winforms" tag this would be duplicate of https://stackoverflow.com/questions/4579506/how-to-do-alignment-within-string-format-c … but `sting.Format` is very strange choice for WinForms where so many rich table layout controls exist. – Alexei Levenkov Apr 11 '19 at 21:35
  • 1
    Try setting a `Monospace` font on the `TextBox`. That should do the trick. – zx485 Apr 11 '19 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. – LarsTech Apr 11 '19 at 21:40
  • Just an FYI, the professor has specifically told us, we cannot change the layout of the form, including the textbox itself, meaning I cannot use datagrids and listviews. – Gentian Gashi Apr 11 '19 at 21:41
  • `How come all the 0's are in the wrong place?` It looks like it displays the way you want, according to your first block. – LarsTech Apr 11 '19 at 21:52
  • ```Name - Grade 1 - Grade 2 - Grade 3 - Final Grade``` I gave this as a general idea. When I'm inputting Alex's class test grade, only the top 0 should change, not everyone elses grades along with it. – Gentian Gashi Apr 11 '19 at 21:55
  • You are talking about code we can't see. We don't know how you populated your array. – LarsTech Apr 11 '19 at 21:57
  • I tested it by doing, ```Grade[0, 0] = 5;``` I get the same result, everyone's grades change for some reason – Gentian Gashi Apr 11 '19 at 22:00
  • You're displaying `Grade[x, x]`, which doesn't make a whole lot of sense. – LarsTech Apr 11 '19 at 22:08
  • The alignment option in string.Format or string interpolation can be used for padding https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated – Slai Apr 12 '19 at 16:37

1 Answers1

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;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user287107
  • 9,286
  • 1
  • 31
  • 47