chrisliando 0 Newbie Poster

Hi..I have a problem with serializing listView and treeView items using VISUAL C++ 2005 EXPRESS EDITION.

I knew that the ListView class has a Serialize method but I don't understand how to use it. Here is my effort to make my own serialization.

Actually I've been able to both serialize and deserialize but I just use TextBox as input. I was very confused with the ListView Items, I don't know the difference between ListItem and ListItemCollection?

Everytime I run the application it went runtime error. I knew that the problem is the ones I mark red below in the code. But I don't know what to change. And for lstEmployees->Add(empl); it must be placed inside the loop / outside the loop?

Can someone help me, please. Thank you so much..

private: System::Void listView1_KeyDown(System:bject^  sender, System::Windows::Forms::KeyEventArgs^  e) {
   if (e->KeyCode == Keys:elete) //delete listView di treeView juga dihapus dan sebaliknya.
             {
               //delete listView items before the real CSV file is physically deleted.
      listView1->Items->RemoveAt(idxlist);
      listView1->Refresh();
      //Serialize
      String ^strFilename = "Employees.csv";
      Employee ^empl = gcnew Employee;
               
      //retrieve from listView
      int count = 0;
      int i = 0;
      while (i < listView1->Items->Count)
       {
      //ListViewItem ^lviEmployee = listView1->Items;
      //lviEmployee->Serialize(listView1->Items, bcrStream);
      //int idxlastitem = listView1->Items->Count;
      if (i == 0) 
     empl->ecompany = listView1->Items->Text;
      else if (i == 1) 
       empl->ename = listView1->Items[count]->SubItems->ToString();
      else if (i == 2)
        empl->eage  = listView1->Items[count]->SubItems->ToString();
      else if (i == 3)
        empl->egender = listView1->Items[count]->SubItems->ToString();
      else if (i == 4)
        empl->eaddress = listView1->Items[count]->SubItems->ToString();
      else if (i == 5)
        empl->ecity = listView1->Items[count]->SubItems->ToString();
      else if (i == 6)
        empl->ezip = listView1->Items[count]->SubItems->ToString();
      else if (i == 7)
        empl->ephone = listView1->Items[count]->SubItems->ToString();                  lstEmployees->Add(empl);
      i++;
      count++;
       } //while i < listView1->Items->Count 
      
      //retrieve from listView

      //lstEmployees->Add(empl);
    
      FileStream ^bcrStream = gcnew FileStream(strFilename, FileMode:penOrCreate, FileAccess::Write, FileShare::Write);
      SoapFormatter ^bcrSoap = gcnew SoapFormatter();
     
      //Serialize di sini ke dalam lstEmployees untuk dipakai update tampilan listView oleh function ShowEmployees.
      bcrSoap->Serialize(bcrStream, lstEmployees);
      bcrStream->Close();

      //setelah Serialize, semua tampilan listView di-refresh.
      UpdatelistView();               
      //Serialize
    }      

   }