2 Replies - 730 Views - Last Post: 25 June 2011 - 10:26 AM Rate Topic: -----

#1 Griffen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 11

converting this code to work with listview

Posted 25 June 2011 - 09:40 AM

Hello Everyone, I have recently found a code that works great with serializing the treeview and saving it to XML then you can load the XML and all settings are restored.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: converting this code to work with listview

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8947
  • View blog
  • Posts: 33,544
  • Joined: 12-June 08

Re: converting this code to work with listview

Posted 25 June 2011 - 10:13 AM

I'm at a loss here. Are you asking for someone to do the conversion, or are you getting a specific problem or exception? If it's the latter what is the problem, what is it doing, what should it be doing, and/or what is the exception and what line is it on?

Please be specific on the issues so we can better help you!
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: converting this code to work with listview

Posted 25 June 2011 - 10:26 AM

What he is asking is that we help him recode it it work with listviews instead of treeviews. What he doesn't understand is that you essentially have to rewrite the entire thing because what it is doing is taking a treeview, loading it into a special custom structure (treeviewdata) and use that to serialize it to and from a file.

To get it to work with listviews, you have to create your own new custom structure for a listview and then it will be serializable in the same way. Windows forms controls themselves are not able to be called on using XmlSerializer.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1