Add files to listview

W

Wim

I'm trying to add files with a OpenFileDialog to a listview. This is what
I've come up with sofar:

protected void ButAddClick(object sender, System.EventArgs e)
{
OpenFileDialog MyDialog = new OpenFileDialog();

MyDialog.Multiselect = true ;
MyDialog.Filter = "WAVE files (*.wav)|*.wav|All files (*.*)|*.*" ;
MyDialog.RestoreDirectory = true ;
MyDialog.Title = "Add Files";

if (MyDialog.ShowDialog() == DialogResult.OK)
* foreach (int i in MyDialog.FileNames())
* listView.Items.Add(MyDialog.FileNames(i), i);
}

But the compiler says: "'System.Windows.Forms.FileDialog.FileNames'
denotes a 'property' where a 'method' was expected" about both lines with
a *.

Could someone explain to me what I'm doing wrong? TIA!
 
P

Philip Rieck

try replacing the lines you have marked with

int i = 0;
foreach (string fName in MyDialog.FileNames)
listView.Items.Add(fName, i++);

or

for( int i; i < MyDialog.FileNames.Count; i++)
listView.Items.Add( MyDialog.FileNames, i);
 
P

Peter Rilling

Not quite sure about your syntax. The FileNames points to an array. The
syntax for accessing an array in C# is wrong. It should be MyDialog

Few things wrong:

1) The FileNames property points to an array. Your syntax for accessing an
array in C# is wrong. It should be MyDialog.FileNames.
2) FileNames is an array of strings. Why are you extracting an integer
from the array in your foreach loop? Try "foreach(string s in
MyDialog.FileNames){listView.Items.Add(s)}".
3) You never use "()" in a property syntax. This is what your system is
probably complaining about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top