0

I'am loading a text file and displaying the textfile name in a listbox. And when I click the listbox im saving the path of the file to a variable. This works in another application of mine, but in the new application I get the following error :

Error 8 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments Error 9 Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

This is the code :

string fileloadpath;

private void FileListbox_SelectedIndexChanged(object sender, EventArgs e)
{
  var selectedItems = FileListbox.SelectedItems.Cast<FileItem>();                   

  var all = string.Join(Environment.NewLine, selectedItems.Select(x => x.Path1));

  fileloadpath = all;
}

Edit : I added ToArray() at the end and that fixed it. Thanks guys.

I have one more error related to this :

When I display the file path in the listbox, instead of the actual file name(test.txt), the text is displayed as : "OpenCV.Form1+FileItem"

Here is the code :

void reciperefresh()
   {
       FileListbox.Items.Clear();

       string[] files = Directory.GetFiles(@"C:\Recipe", "*.txt", 
          SearchOption.AllDirectories);

       foreach (string f in files)
       {
           var fileItem = new FileItem { Title1 = Path.GetFileName(f), 
            Path1 = Path.GetFullPath(f) };
           FileListbox.Items.Add(fileItem);

       }
   }
6
  • what is type of fileloadpath ? Commented Apr 6, 2018 at 10:35
  • 3
    .ToArray() on the end of your query. Commented Apr 6, 2018 at 10:36
  • selectedItems.Select(x => x.Path1) will return an IEnumerable. You need to call .ToArray() on that select Commented Apr 6, 2018 at 10:37
  • Hi, adding ToArray() worked. I have one more query related to the same question. Have updated the main question. Commented Apr 6, 2018 at 10:51
  • @user3177511 I have updated my answer to address your second issue (which should really be a new question) Commented Apr 6, 2018 at 10:54

5 Answers 5

2

Try to replace selectedItems.Select(x => x.Path1) with selectedItems.Select(x => x.Path1).ToArray(). You should pass an array of strings instead of IEnumerable<string>.

Sign up to request clarification or add additional context in comments.

5 Comments

I have one more query related to this, have updated the question.
@user3177511 override the ToString() method for your FileItem class. If I remember correctly it will be called automatically and will display in the way you write.
Thank you, both your method as well as @Owen's answer worked.
Hi, There is a new error now, when i click on any item in the list box it says "Unable to cast object of type 'System.String' to type 'FileItem'." File item is public string Title1 { get; set; } and public string File1 { get; set; }
@user3177511 create new question and describe the problem with it's exception. And add there the Click event handler that you have
1

Select returns an IEnumerable but the method requires a string[]. Add ToArray after the Select.

var all = string.Join(Environment.NewLine, selectedItems.Select(x => x.Path1).ToArray());

Re: the additional part to your question (which should really be a new question), you need to change it to:

FileListbox.Items.Add(fileItem.Title1);

Otherwise it is using the Object.ToString() method which you are not overriding. Alternatively override the ToString method in your FileItem class.

3 Comments

Perfect. Thank you very much @Owen
There is a new error now, when i click on any item in the list box it says "Unable to cast object of type 'System.String' to type 'FileItem'." File item is public string Title1 { get; set; } and public string File1 { get; set; }
It seems that this new error is because of using Title1 in FileListbox.Items.Add(fileItem.Title1); If I remove Title1, this new error goes away but the file name is incorrect.
1

You need to pass an Array or Strings instead of the Enumerable that your selectedElements are.

Comments

1

The error is that you are trying to use an enumeration of string instead of a string array. Try that:

string.Join(Environment.NewLine, selectedItems.Select(x => x.Path1).ToArray());

Comments

0

The error is self explanatory. It is saying to convert it to array of string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.