Adding lines from an array to a Window Form list view

Asked

Viewed 117 times

0

I am doing a project to solve the binomial of Newton, this part is easy I managed to solve the problem on the console the way I wanted, now I am migrating to Windows Form and I have a problem to print the Pascal triangle in a listbox. I want to print on this graduate

[1]

[1][1]

[1][2][1]

[1][3][3][1]

[1][4][6][4][1]

But I can’t do it like that in the list box, he ends up printing one on top of the other.

[1]

[1]

[1]

[1]

[2]

[1] The code I used to make on the console.

int n;
   Console.WriteLine("Digite o valor de  N");
                n = int.Parse(Console.ReadLine());
                n = n + 1;
                matriz_triangulo = new int[n, n];
  Console.WriteLine("Digite o valor de  N");
                n = int.Parse(Console.ReadLine());
                n = n + 1;
                matriz_triangulo = new int[n, n];



            for (int linha = 0; linha < n; linha++)
            {
                for (int coluna = 0; coluna < n; coluna++)
                {
                    if (linha == coluna || coluna == 0)
                    {
                        matriz_triangulo[linha, coluna] = 1;
                    }
                    else if (linha != 0 && coluna != 0)
                    {
                        matriz_triangulo[linha, coluna] = matriz_triangulo[linha - 1, coluna - 1] + matriz_triangulo[linha - 1, coluna];
                    }
                }
            }
            for (int linha = 0; linha < n; linha++)
            {
                for (int coluna = 0; coluna < n; coluna++)
                {
                    if (matriz_triangulo[linha, coluna] != 0 && matriz_triangulo[linha, coluna] != 0)
                        Console.Write("[" + matriz_triangulo[linha, coluna] + "] ");
                }
                Console.WriteLine();
            }

1 answer

0

Solution

for (int linha = 0; linha < n; linha++)
            {
                string linhaListBox = "";
                for (int coluna = 0; coluna < n; coluna++)
                {
                    if (triangulo_pascal[linha, coluna] != 0 && triangulo_pascal[linha, coluna] != 0)
                        linhaListBox += "[" + triangulo_pascal[linha, coluna] + "] ";
                }
                lbx_trianguloPascal.Items.Add(linhaListBox);
            }

Browser other questions tagged

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