Question

I want to display a matrix as shown below in a textbox, but I don't know where to start. How can I get this result in a textbox?

 5  5  5  5
 8  9  3  1
 7  9  2  7 
 3  7  8  6
 8  3  1  2
Was it helpful?

Solution

int[,] matrix = new int[,] {{5, 5, 5, 5},
                            {8, 9, 3, 1},
                            {7, 9, 2, 7},
                            {3, 7, 8, 6},
                            {8, 3, 1, 2}};

You will need to create a multiline TextBox to show this information. Then use something like this to write the matrix to the TextBox:

string matrixString = "";
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        matrixString += matrix[i, j].ToString();
        matrixString += " ";
    }

    matrixString += Environment.NewLine;
}
this.textBoxName.Text = matrixString;

You get this result:

Result

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top