Go through a ListBox and put its items in an array

5

I fill a list from OpenFileDialog and I put it in "file" and then put it on the list like this:

foreach (string file in selectedFiles)
{
    ListBoxItem lstItem = new ListBoxItem();
    lstItem.Content = file; //File es la cadena de la URL
    miLista.Items.Add(lstItem);
} 

What I assign to ListBox are URL type

c: \ user \ paco \ imagen1.jpg

c: \ user \ paco \ image2.jpg

All right up to here, but now I need to go through that ListBox and enter their items in a String[]

String[] matriz = new String[miLista.Items.Count];
foreach (Object index in miLista.Items)
{
    matriz = index.ToString().Split();
} 

In this way what gets me into each index of string[] is the following:

System.Windows.Controls.ListBoxItem: c: \ user \ cucumber \ image1.jpg System.Windows.Controls.ListBoxItem: c: \ user \ cucumber \ image2.jpg System.Windows.Controls.ListBoxItem: c: \ user \ cucumber \ image3.jpg

What I need is only the URL and that I do not return the Type and the Value, I need to put in the String[] only the value.

I fill ListBox like this:

OpenFileDialog myFD = new OpenFileDialog();
myFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
myFD.Multiselect = true;
myFD.Title = "Seleccione uno o más archivos";
myFD.Filter = "Archivos imagen (*.jpg),(*.jpeg),(*.gif),(*.png)|*.jpg;*.jpeg;*.gif;*.png";

Nullable<bool> result = myFD.ShowDialog();
if (result == true)
{
    string[] selectedFiles = myFD.FileNames; // La propiedad FileNames (o FileName) almacena la ruta, nombre y extensión.
    foreach (string file in selectedFiles)
    {
        miLista.Items.Add(file);               
    }
    archivo.set_listaReproduccion(selectedFiles);
    archivo.grabar();
}
    
asked by Fran Pino 22.04.2016 в 13:46
source

2 answers

4

The problem you have is in this portion of code:

foreach (string file in selectedFiles)
{
    ListBoxItem lstItem = new ListBoxItem();
    lstItem.Content = file; //File es la cadena de la URL
    //aqui estas agregando un ListBoxItem a Items cuando solo deberías agregar file
    miLista.Items.Add(lstItem); 
} 

One solution would be to replace this line and use the suggestions we gave you before:

 miLista.Items.Add(lstItem);

By

 miLista.Items.Add(file);

But if you need lstItem to follow within the listbox.Items collection then what you have to do is a cast.

String[] matriz = new String[miLista.Items.Count];
for (int i = 0; i < miLista.Items.Count; i++)
{
    ListBoxItem item = miLista.Items[i] as ListBoxItem;
    if(item!= null)
    {
        //suponiendo que content es un string
        matriz[i] = miLista.Items[i].Content;
    }

} 

Assuming you load your listbox in this way, you should not have casting problems:

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (var item in openFileDialog1.FileNames)
            {
                listBox1.Items.Add(item);
            }
        }

        String[] matriz = new String[listBox1.Items.Count];

        listBox1.Items.CopyTo(matriz, 0);
    }

Notice that FileNames is already a String[] you hardly need to do neither the copy nor the for only assign it:

String[] matriz = openfiledialog.Filenames;

But anyway I will leave below ways to do it so it can serve other people.

The simplest option is:
listBox1.Items.CopyTo(matriz, 0);

But you can also do it using a cycle for instead of foreach

String[] matriz = new String[miLista.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
    matriz[i] = listBox1.Items[i].ToString();
}
    
answered by 22.04.2016 / 14:18
source
2

You can try using:

foreach (string file in selectedFiles)
{
    miLista.Items.Add(file); 
    // No es necesaria la instancia de ListBoxItem ya que puedes agregar
    // objects de cualquier tipo.
}

And then:

string[] Matriz = miLista.Items.Cast<string>().ToArray();
// Asignas el valor directamente.

With this you directly have an array of string with all the values.

EDIT: I clarify that Cast<string>() should not be necessary since we are adding items of type string to list.

If the above does not work for you, you can try to combine Alan's answer and this one:

string[] Matriz = new string[miLista.Items.Count];
for (int i = 0; i < miLista.Items.Count; i++)
    Matriz[i] = miLista.Items[i].ToString();

This is supposed to work, since you have elements of the string type within ListBox.Items

  

If none works for you, you have an array of strings in OpenFileDialog that is FileNames that is already type string[] you should only do this:

string[] Matriz = ofd.FileNames; // donde ofd es tu OpenFileDialog.
foreach (string s in Matriz)
    miLista.Items.Add(s); // Agrega al listbox los elementos de la Matriz.

Sometimes the order of instructions may be wrong, but it can work in any way.

Here I leave you the answer of SO where I found something similar, greetings!

    
answered by 22.04.2016 в 14:15