开发者

Listview Serialization !

开发者 https://www.devze.com 2022-12-11 06:45 出处:网络
appreciate i开发者_开发百科f anyone knows how to serialize a listview using vb.netYou can create an inherited version of the ListView control that is serializable using the ISerializable interface. Th

appreciate i开发者_开发百科f anyone knows how to serialize a listview using vb.net


You can create an inherited version of the ListView control that is serializable using the ISerializable interface. This allows you to customize which properties of the control you want to serialize and deserialize. You may have to play around with this a bit to get the desired results.

Edit: Here is the VB.NET version. I left the C# version posted below.

 <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);
        }

    }


 }


I think the vb.net code should inherit Listview, not implement it.

and also I think the GetObjectData method should implement ISerializable.GetObjectData

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号