2009-11-07 2 views

ответ

0

Вы можете создать унаследованную версию элемента управления ListView, который можно сериализовать, используя интерфейс ISerializable. Это позволяет вам настроить, какие свойства элемента управления вы хотите сериализовать и десериализовать. Возможно, вам придется немного поиграть, чтобы получить желаемые результаты.

Редактировать: Вот версия VB.NET. Я оставил версию C#, опубликованную ниже.

<Serializable()> _ 
Public Class MyListView 
    Implements ISerializable 
    Implements ListView 

    Public Sub New() 
    End Sub 

    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) 

     'Clear out the current items in the ListViewItems if there are any 
     MyBase.Items.Clear() 

     'Retrieve the List of ListViewItems you created on Serialization 
     Dim items As List(Of ListViewItem) = info.GetValue("ListViewItems", typeof(List<ListViewItem>)) as List(Of ListViewItem); 

     'Add each ListViewItem back into the collection 
     For Each item As ListViewItem In items 
      MyBase.Items.Add(item) 
     Next 

    End Sub 

    Public Overridable Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) 

     'Create a List of ListViewItems for serialization 
     Dim items As List(Of ListViewItem) = New List(Of ListViewItem)() 

     'Add each ListViewItem to the collection 
     For Each item As ListViewItem In MyBase.Items 
      items.Add(item) 
     Next 

     'Add the collection to the SerializationInfo object 
     info.AddValue("ListViewItems", items) 


    End Sub 
End Class 

C#:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Runtime.Serialization; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    [Serializable] 
    public class SerializableListView : ListView, ISerializable 
    { 
     public SerializableListView() 
     { 

     } 

     //This method is called when the object is deserialized 
     protected SerializableListView(SerializationInfo info, StreamingContext context) 
     { 
      //Clear out the current items in the ListViewItems if there are any 
      base.Items.Clear(); 

      //Retrieve the List of ListViewItems you created on Serialization 
      List<ListViewItem> items = (List<ListViewItem>)info.GetValue("ListViewItems", typeof(List<ListViewItem>)); 

      //Add each ListViewItem back into the collection 
      foreach (ListViewItem item in items) 
      { 
       base.Items.Add(item); 
      } 
     } 

     //This method is called when the object is Serialized 
     public virtual void GetObjectData(SerializationInfo info, StreamingContext context) 
     { 
      //Create a List of ListViewItems for serialization 
      List<ListViewItem> items = new List<ListViewItem>(); 

      //Add each ListViewItem to the collection 
      foreach (ListViewItem item in base.Items) 
      { 
       items.Add(item); 
      } 

      //Add the collection to the SerializationInfo object 
      info.AddValue("ListViewItems", items); 
     } 

    } 


} 
0

Я думаю, что код vb.net должен наследовать Listview, не реализовать.

а также я думаю, что метод GetObjectData должен реализовывать ISerializable.GetObjectData

Смежные вопросы