473,287 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Still trying to serialize treeview in C#

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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"meh" <no*************@cox.net> wrote in message
news:eD**************@tk2msftngp13.phx.gbl...
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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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/de...ingobjects.asp >
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "meh" <no*************@cox.net> wrote in message
> news:e8**************@TK2MSFTNGP12.phx.gbl...
>> 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
>>
>>
>
>




Nov 16 '05 #1
0 3408

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: serge calderara | last post by:
Dear all, I am looking for a solution to serialize complete treeview nodes structure in a file or database or XML and deserialize it back when needed. this to save the current nodes...
4
by: Wayne Wengert | last post by:
I am still stuck trying to create a Class to use for exporting and importing array data to/from XML. The format of the XML that I want to import/export is shown below as is the Class and the code I...
3
by: Toxick | last post by:
Hello experts, I'm a total C# noob with a total C# noob question. I've been Serializing in C++ (MFC) and writing data to std::fstreams for quite some time, so maybe I'm not understanding...
1
by: Rog | last post by:
Hello, Yesterday I downloaded IEwebcontrols.exe and TreeviewControl.msi from http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dnaspp/html/aspnet-...
0
by: JasonKendall | last post by:
I have an object that I want to serialize to a string variable. I want to use the SoapFormatter, for readability. The serialization is working great and I'm pleased with the results....
2
by: Jack | last post by:
Hello, I am trying use a TreeView with checkboxes. I would like to check more than one node and allow all child nodes of selected nodes to be checked or unchecked with the parent is checked. ...
0
by: banduraj | last post by:
I get an System.InvalidOperationException when trying to Serialize a struct that has a type refrenced from another library. The exception is: System.InvalidOperationException was unhandled...
2
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I have a control "tv_ClientMain" that populates with nodes of system information, software, hardware etc. I would like to serialize it from the...
11
by: Kenneth McDonald | last post by:
Any guesses as to how many people are still using Tkinter? And can anyone direct me to good, current docs for Tkinter? Thanks, Ken
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.