Still trying to serialize treeview in C#

M

meh

Still have not been able to convert this. More importently.....Is this
sample going about it the right way???

tia
meh

Here is the vb code.........I keep looking at older vb.net projects to try
and figure out how to teach myself C# based on my old vb code but it's been
more difficult than I first thought. I'm finding that it would be better if
didn't know any previous programming language.


Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap

Public Class MySerializer

<Serializable()> Structure sNode
Dim level As Integer
Dim node As String
Dim Mytag As String
Dim MyImageIndex As Integer
Dim MySelectedImageIndex As Integer

End Structure

Dim arrTreeNodes As New ArrayList()

#Region " Create Node List "

Sub CreateNodeList(ByVal node As TreeNode, ByVal fName As String)

Static level As Integer
Dim currNode As TreeNode
Dim myNode As sNode
Application.DoEvents()
myNode.level = level
myNode.node = node.Text
myNode.Mytag = node.Tag
myNode.MyImageIndex = node.ImageIndex
myNode.MySelectedImageIndex = node.SelectedImageIndex

arrTreeNodes.Add(myNode)
If node.Nodes.Count > 0 Then
level = level + 1
For Each currNode In node.Nodes
CreateNodeList(currNode, fName)
Next
level = level - 1
End If
End Sub

#End Region

#Region " Save Nodes "

Sub SaveNodesBinary(ByVal fName As String)

Dim bf As BinaryFormatter
Dim fs As FileStream
fs = File.Create(fName)
bf = New BinaryFormatter()
bf.Serialize(fs, arrTreeNodes)
fs.Close()

End Sub
'
Sub SaveNodesXML(ByVal fName As String)

Dim sf As SoapFormatter
Dim fs As FileStream
fs = File.Create(fName)
sf = New SoapFormatter()
sf.Serialize(fs, arrTreeNodes)
fs.Close()

End Sub

#End Region

#Region " Load Nodes "

Sub LoadNodesBinary(ByVal CurrTree As TreeView, ByVal fName As String)
CurrTree.Nodes.Clear()
Dim bf As BinaryFormatter
Dim fs As FileStream
fs = File.Open(fName, FileMode.Open)
bf = New BinaryFormatter()
arrTreeNodes = CType(bf.Deserialize(fs), ArrayList)
fs.Close()
showNodes(CurrTree)
End Sub
'
Sub LoadNodesXML(ByVal CurrTree As TreeView, ByVal fName As String)
CurrTree.Nodes.Clear()
Dim sf As SoapFormatter
Dim fs As FileStream
fs = File.Open(fName, FileMode.Open)
sf = New SoapFormatter()
arrTreeNodes = CType(sf.Deserialize(fs), ArrayList)
fs.Close()
showNodes(CurrTree)
End Sub

#End Region

#Region " Show Nodes "

Sub showNodes(ByVal CurrTree As TreeView)
Try
Dim o As Object
Dim currNode As TreeNode
Dim level As Integer = 0

Dim i As Integer
For i = 0 To arrTreeNodes.Count - 1
o = arrTreeNodes(i)
Dim oNode As sNode = CType(o, sNode)

If oNode.level = level Then
If currNode Is Nothing Then
currNode = CurrTree.Nodes.Add(oNode.node.ToString)
currNode.ImageIndex = (oNode.MyImageIndex.ToString)
currNode.SelectedImageIndex =
(oNode.MySelectedImageIndex.ToString)
If Not oNode.Mytag = Nothing Then
' If the tag is not Null
currNode.Tag = (oNode.Mytag.ToString)
' Add the tag
End If
' End
Else
If level = 0 Then
currNode =
CurrTree.Nodes.Add(oNode.node.ToString)
currNode.ImageIndex =
(oNode.MyImageIndex.ToString)
currNode.SelectedImageIndex =
(oNode.MySelectedImageIndex.ToString)
If Not oNode.Mytag = Nothing Then
' If the tag is not Null
currNode.Tag = (oNode.Mytag.ToString)
' Add the tag
End If
' End
Else
currNode =
currNode.Parent.Nodes.Add(oNode.node.ToString) ' This line prevents Root
nodes
currNode.ImageIndex =
(oNode.MyImageIndex.ToString)
currNode.SelectedImageIndex =
(oNode.MySelectedImageIndex.ToString)
If Not oNode.Mytag = Nothing Then
' If the tag is not Null
currNode.Tag = (oNode.Mytag.ToString)
' Add the tag
End If
' End
End If
' End
End If

Else
If oNode.level > level Then
currNode = currNode.Nodes.Add(oNode.node.ToString)
currNode.ImageIndex = (oNode.MyImageIndex.ToString)
currNode.SelectedImageIndex =
(oNode.MySelectedImageIndex.ToString)
If Not oNode.Mytag = Nothing Then
' If the tag is not Null
currNode.Tag = (oNode.Mytag.ToString)
' Add the tag
End If
' End
level = oNode.level
Else
While oNode.level < level
currNode = currNode.Parent
level = level - 1
End While
If level = 0 Then
currNode =
CurrTree.Nodes.Add(oNode.node.ToString)
currNode.ImageIndex =
(oNode.MyImageIndex.ToString)
currNode.SelectedImageIndex =
(oNode.MySelectedImageIndex.ToString)
If Not oNode.Mytag = Nothing Then
' If the tag is not Null
currNode.Tag = (oNode.Mytag.ToString)
' Add the tag
End If
' End
Else
currNode =
currNode.Parent.Nodes.Add(oNode.node.ToString) ' This
line prevents Root nodes
currNode.ImageIndex =
(oNode.MyImageIndex.ToString)
currNode.SelectedImageIndex =
(oNode.MySelectedImageIndex.ToString)
If Not oNode.Mytag = Nothing Then
' If the tag is not Null
currNode.Tag = (oNode.Mytag.ToString)
' Add the tag
End If
' End
End If
End If
End If

Application.DoEvents()
Next
CurrTree.Nodes(0).Expand()

Catch LoadException As Exception
MsgBox("Error in loading nodes! " & vbCrLf &
LoadException.Message)
End Try

End Sub

#End Region

End Class


Nicholas Paldino said:
meh,

If you provide the VB.NET code, it should not be difficult at all to
provide the equivalent C# code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

meh said:
Thx Nicholas i did it in VB.net. I would be more than willing to share the
code so as to help me understand how to convert from vb to c# dotNet. I
found a vb.net to c#.net converter on the web but the converted code does
not work.
Thanks again I will have a look at the link you provided.

meh

message news:%[email protected]...
meh,

How did you do it in VB? Are you talking about VB6 or VB.NET? In
VB.NET (or C#), you will have to use the Serialization attribute, possibly
in conjunction with the ISerializable interface. Check out the section of
the .NET framework titled "Serializing Objects", located at (watch for
line
wrap):

http://msdn.microsoft.com/library/d...n-us/cpguide/html/cpovrserializingobjects.asp

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to be able to persist a treeView to disk. I would like to save
the
node text, tag, image and selected image. I have done this using vb but
I
cant seem to translate from vb to vc#.
Can ne1 point me to documentation or samples that might help???
Thanks in advance


meh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top