Problem is I am trying to convert this to work with the listview aswell. I have tried replacing the treeview data with listview data, the original code is:
Option Strict On ''' <summary> ''' The TreeViewDataAccess class allows the nodes within a TreeView to be ''' persisted to xml for later retrevial. ''' </summary> Public Class TVDA #Region "Structures" ''' <summary> ''' TreeViewData structure represents the root node collection of a TreeView ''' and provides the PopulateTreeView function to add these nodes to a specified ''' TreeView instance. ''' </summary> <Serializable()> Public Structure TreeViewData ''' <summary>Array of TreeNodeData objects representing the root nodes in a TreeView.</summary> Public Nodes() As TreeNodeData ''' <summary> ''' Creates new instance of the TreeViewData structure based from the ''' specified TreeView. ''' </summary> ''' <param name="treeview">TreeView to build the TreeViewData instance from.</param> Public Sub New(ByVal treeview As TreeView) 'Check to see if there are any root nodes in the TreeView If treeview.Nodes.Count = 0 Then Exit Sub 'Populate the Nodes array with child nodes ReDim Nodes(treeview.Nodes.Count - 1) For i As Integer = 0 To treeview.Nodes.Count - 1 Nodes(i) = New TreeNodeData(treeview.Nodes(i)) Next End Sub ''' <summary> ''' Populates the specified TreeView with the current TreeViewData instance. ''' </summary> ''' <param name="treeview">TreeView instance to populate.</param> Public Sub PopulateTree(ByVal treeview As TreeView) 'Check to see if there are any root nodes in the TreeViewData If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit Sub 'Populate the TreeView with child nodes treeview.BeginUpdate() For i As Integer = 0 To Me.Nodes.Length - 1 'treeview.Nodes.Add(Me.Nodes(i).ToTreeNode) treeview.Nodes.Add(LCase(Me.Nodes(i).ToTreeNode.Text), Me.Nodes(i).ToTreeNode.Text) Next treeview.EndUpdate() End Sub End Structure ''' <summary> ''' TreeNodeData structure represents a TreeNode and provides the ''' ToTreeNode function to convert the instance to a TreeNode object. ''' </summary> <Serializable()> Public Structure TreeNodeData ''' <summary>String representing the Text property of the TreeNode.</summary> Public Text As String ''' <summary>Integer representing the ImageIndex property of the TreeNode.</summary> Public ImageIndex As Integer ''' <summary>Integer representing the SelectedImageIndex property of the TreeNode.</summary> Public SelectedImageIndex As Integer ''' <summary>Boolean representing the Checked state of the TreeNode.</summary> Public Checked As Boolean ''' <summary>Boolean representing the Expanded state of the TreeNode.</summary> Public Expanded As Boolean ''' <summary>Object representing the Tag property of the TreeNode.</summary> Public Tag As Object ''' <summary>Array of TreeNodeData objects representing the root nodes in a TreeView.</summary> Public Nodes() As TreeNodeData ''' <summary> ''' Creates new instance of the TreeNodeData structure based on the specified TreeNode. ''' </summary> ''' <param name="node">TreeNode to build the TreeNodeData instance from.</param> Public Sub New(ByVal node As TreeNode) 'Set the basic TreeNode properties Me.Text = node.Text Me.ImageIndex = node.ImageIndex Me.SelectedImageIndex = node.SelectedImageIndex Me.Checked = node.Checked Me.Expanded = node.IsExpanded 'See if there is an object in the tag property and if it is serializable If (Not node.Tag Is Nothing) AndAlso node.Tag.GetType.IsSerializable Then Me.Tag = node.Tag 'Check to see if there are any child nodes If node.Nodes.Count = 0 Then Exit Sub 'Recurse through child nodes and add to Nodes array ReDim Nodes(node.Nodes.Count - 1) For i As Integer = 0 To node.Nodes.Count - 1 Nodes(i) = New TreeNodeData(node.Nodes(i)) Next End Sub ''' <summary> ''' Returns as TreeNode built from the instance of the TreeNodeData object. ''' </summary> Public Function ToTreeNode() As TreeNode 'Create TreeNode based on instance of TreeNodeData and set basic properties ToTreeNode = New TreeNode(Me.Text, Me.ImageIndex, Me.SelectedImageIndex) ToTreeNode.Checked = Me.Checked ToTreeNode.Tag = Me.Tag If Me.Expanded Then ToTreeNode.Expand() 'Recurse through child nodes adding to Nodes collection If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit Function For i As Integer = 0 To Me.Nodes.Length - 1 ToTreeNode.Nodes.Add(LCase(Me.Nodes(i).ToTreeNode.Text), Me.Nodes(i).ToTreeNode.Text) Next End Function End Structure #End Region #Region "Public" Public Shared Sub LoadTVData(ByVal treeView As TreeView, ByVal data As String) Try Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData)) Dim reader As New System.Xml.XmlTextReader(New System.IO.StringReader(data)) Dim treeData As TreeViewData = CType(ser.Deserialize(reader), TreeViewData) treeData.PopulateTree(treeView) reader.Close() Catch End Try End Sub Public Shared Sub SaveTVData(ByVal treeView As TreeView) Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData)) Dim writer As New System.IO.StringWriter ser.Serialize(writer, New TreeViewData(treeView)) textbox1.text = writer.ToString writer.Close() End Sub #End Region End Class
Any help with this problem would be appreciated, thank you.

New Topic/Question
Reply



MultiQuote







|