Sergio Tapia
Sergio Tapia

Reputation: 41138

Cannot add items to listbox

private void DisplayFiles()
{
    lstPhotos.Items.AddRange(files);
}

files is a List This gives this error:

cannot convert from 'System.Collections.Generic.List' to 'object[]'

Which makes sense. What should I do?

Upvotes: 0

Views: 346

Answers (2)

Josh
Josh

Reputation: 69262

private void DisplayFiles()
{
    lstPhotos.Items.AddRange(files.ToArray());
}

That should work. You could also bind the list to the listbox which is the preferred way of doing it in WPF and Windows Forms.

lstPhotos.DataSource = files; // Windows Forms
lstPhotos.ItemsSource = files; // WPF

Upvotes: 1

Adam P
Adam P

Reputation: 4703

Try this instead:

private void DisplayFiles()
{
    lstPhotos.Items.AddRange(files.ToArray<object>);
} 

Upvotes: 2

Related Questions