Pass elements from a listbox to a two-dimensional array [closed]

-1

What I want to do is pass the elements stored in a listbox to a two-dimensional array to later calculate centroids, I've tried but I go line by line instead of element by element

OpenFileDialog abrir = new OpenFileDialog();
   String ruta;

        abrir.Title = "Seleccionar fichero";

        abrir.Filter = "Documentos de texto  (*.txt)|*.txt" + "|Todos los archivos (*.*)|*.* ";
        abrir.FileName = this.boxexaminar.Text;
        if (abrir.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.boxexaminar.Text = abrir.FileName;
        }

        ruta = boxexaminar.Text;

        StreamReader sr = new StreamReader(Convert.ToString(ruta));
        while (sr.Peek() >= 0)
        {
            showdata.Items.Add(Convert.ToString(sr.ReadLine()));

        }
        sr.Close();

        String[] matriz = new String[showdata.Items.Count];
        for (int i = 0; i < showdata.Items.Count; i++)
        {
            matriz[i] = showdata.Items[i].ToString();
        }

        showclases.Text = Convert.ToString(matriz[0]);
    
asked by Angel Sanchez Lugo 11.10.2017 в 03:38
source

1 answer

0

Good Angel,

Analyzing what you comment on the question and subsequent comments, you could do this process without using the ListBox showdata .

As you say, your file .txt has a structure of numbers separated by spaces ' ' , knowing this, you can read all the lines of the file and make a Split adding all the results to a List to convert it to a array later, since you can not add data to a array directly:

string[] matriz;
StreamReader sr = new StreamReader(Convert.ToString(ruta));
List<string> list = new List<string>();
while (!sr.EndOfStream) //Mientras no llegue al final del archivo leemos
{
   list.AddRange(sr.ReadLine().Split(' '));
}
sr.Close();
matriz = list.ToArray();
    
answered by 13.10.2017 / 11:32
source