How to Print an Array in Windows Forms of Visual Studio?

Asked

Viewed 822 times

2

How can I print an array in windows Forms? I tried by listbox using this code:

 public void Mostrar_Grafo(int Qtdlinha,int Qtdcoluna, string[,] MAdjacencia)
    {
        ListBox listbox1 = new ListBox();
        listbox1.Size = new System.Drawing.Size(400, 400);
        listbox1.Location = new System.Drawing.Point(10, 10);
        this.Controls.Add(listbox1);
        listbox1.MultiColumn = true;
        listbox1.BeginUpdate();

        listbox1.Items.Add("Matriz de Adjacencia");
        listbox1.Items.Add("");

        for (int i = 0; i < Qtdlinha; i++)
        {
            for (int j = 0; j < Qtdcoluna; j++)
            {
                listbox1.Items.Add(" " + MAdjacencia[i, j] + " ");
            }
        }
    }

But it keeps showing the numbers in a disorganized way as shown in the figure below:

inserir a descrição da imagem aqui

i need you to show in matrix format like this example:

0 1 0 0 0 0 0
1 0 1 0 0 0 0
0 1 0 1 1 1 0
0 0 1 0 1 1 0
0 0 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 0 0 1 1

1 answer

4


You need to assemble the array row before adding it to listbox:

for (int i = 0; i < Qtdlinha; i++)
{
    string linha = "";

    for (int j = 0; j < Qtdcoluna; j++)
    {
        linha += " " + MAdjacencia[i, j] + " ";
    }

    listbox1.Items.Add(linha);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.