display multiple array by messagebox.Show on c# by clicking a button

Asked

Viewed 86 times

0

people are creating a program to consult suppliers(being N suppliers), and I need that by clicking the button can see all registered suppliers and their information as this below :

nomefornecedor1  telefone1  valordoproduto1 nomefornecedor2  telefone2 valordoproduto2
    .              .           .
    .              .           .
    .              .           .

as I declared the vectors

public partial class Form1 : Form
{
        string[] Nome = new string[n];               
        string[] Telefone = new string[n];
        float[] ValorCompra = new float [n];`

and how I’m trying to show by clicking the button

private void buttonfornecedores_Click(object sender, EventArgs e)
{
   int linha;    
   {
      MessageBox.Show(Nome[0],Telefone[0],valorcompra[0]);

   }

 }

I don’t know much about ,then any help thank you

1 answer

0

If you don’t know the amount of elements that will enter your vectors, use List instead of this way (that doesn’t even compile).

List<string> Nomes = new List<string>();
List<string> Fornecedores = new List<string>();
List<string> telefones = new List<string>();

You don’t show how to fill these vectors, so I’ll take into account that you fill them at the same time and that they all have the same amount of items, so your implementation should be like this:

        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("Nome - Fornecedor - Telefone");

            for (int i = 0; i < Nomes.Count; i++)
            {
                stringBuilder.AppendLine($"{Nomes[i]} - {Fornecedores[i]} -{Telefones[i]}");
            }

            MessageBox.Show(stringBuilder.ToString());           

        }

It’s not within the scope of the question, but you could replace the lists with a struct, so you have better control and organization of things

Browser other questions tagged

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