appreciate if anyone knows how to serialize a listview using vb.net
A:
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
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);
}
}
}
Jtangowski
2009-11-07 15:47:10