0

In DocumentsPanel I have a few open forms, each of them is of another kind. How do I save the files that were open in a loop.

In this case, this works.

   using (StreamWriter file = new StreamWriter ("files.txt"))
         {

             foreach (FormEditor doc in dockPanel1.Documents)
             {
                     file.WriteLine (doc.SuperFileName);
             }
          }
   file.Close ();

However, if a second FormBrowser is opened in the panel, error pops up when you try to save the documents. The error is stated below:

    Unable to cast object of type 'App1.FormBrowser' to type 'App1.FormEditor'.
nevets
  • 4,631
  • 24
  • 40

1 Answers1

0

The question is very unclear about what is asked, and very unclear about the classes used in the code.

From observation, both FormEditor and FormBrowser are Document in your DocumentsPanel, and they would be listed in Documents property of DocumentsPanel instance, in your case is dockPanel1, if they exist.

Therefore, when you want to access a document and save it, you need to verify if the document is in the correct type, i.e. if it's in FormEditor. A simple fix could be:

using (StreamWriter file = new StreamWriter ("files.txt"))
{
    foreach (var doc in dockPanel1.Documents)
    {
        var fileEditor = doc as FormEditor;

        if (fileEditor != null)
        {
            file.WriteLine (doc.SuperFileName);
        }
    }

    file.Close();
}
nevets
  • 4,631
  • 24
  • 40