Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Save/Load Treeview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mr.D - 20 Oct 2004 11:47 GMT
How do I save/load the contents of a Treeview to a file?
I have found several good examples written i VB6, but not a single one for
VB.NET.

Please help.

----
Tim
Cor Ligthert - 20 Oct 2004 11:55 GMT
Mr.D,

There are so much samples for loading a Treeview, I think you need a very
special when no one did fit you.

What is so special that no one was good?

Cor
Mr.D - 20 Oct 2004 12:25 GMT
> There are so much samples for loading a Treeview, I think you need a very
> special when no one did fit you.

I diddn't find a single Load+Save example for VB.NET

> What is so special that no one was good?
Nothing special. I dont really care how the contenst are stored.

I've searched on
http://www.planet-source-code.com/
http://www.google.com/
and the Community search in MSDN 2005 Express and I came up whith nothing.

As said, I found many good VB6 examples, but I dont have the skilles of
rewriting them in .NET.
That's why I cried out for help in here.

----
Tim
Cor Ligthert - 20 Oct 2004 12:45 GMT
Mr. D.

The threeview is not really a file related control, when you want to do it
you have to take the nodes from the tree and write them, therefore you will
probably not get easily a sample.

However what you ask looks directly to using a treeview with xmldoc

Beneath the links where I have searched this newsgroup for "treeview and
xml", there should be an example between it. It do not have it myself at the
moment.

http://tinyurl.com/4kpmb

I hope this helps?

Cor

"Mr.D" <kjhh@dlkhdlhjd.com>
...
>> There are so much samples for loading a Treeview, I think you need a very
>> special when no one did fit you.
[quoted text clipped - 15 lines]
> ----
> Tim
thomas wenning - 20 Oct 2004 12:59 GMT
> > There are so much samples for loading a Treeview, I think you need a very
> > special when no one did fit you.
[quoted text clipped - 15 lines]
> ----
> Tim

Hi Tim,

here is an example inC#
http://www.codeproject.com/cs/miscctrl/loadandsave.asp
with http://www.kamalpatel.net/ConvertCSharp2VB.aspx convert it to VB.net

Greeting

Thomas
Mr.D - 20 Oct 2004 14:37 GMT
> here is an example inC#
> http://www.codeproject.com/cs/miscctrl/loadandsave.asp

Thanks Thomas.
I don't know C# but it looks very promissing indeed.

>  with http://www.kamalpatel.net/ConvertCSharp2VB.aspx convert it to VB.net

The converter do convert the code, but unfortunatly I get errors when
pasting the code into VB.

----
Tim
thomas wenning - 21 Oct 2004 10:25 GMT
> > here is an example inC#
> > http://www.codeproject.com/cs/miscctrl/loadandsave.asp
[quoted text clipped - 9 lines]
> ----
> Tim

Hi Tim,

you can bind this c#-control to your project.

Regards

Thomas Wenning
Mr.D - 21 Oct 2004 11:21 GMT
> you can bind this c#-control to your project.

Bind a .cs file to a VB project?

----
Tim
Tym - 20 Oct 2004 12:29 GMT
>How do I save/load the contents of a Treeview to a file?
>I have found several good examples written i VB6, but not a single one for
>VB.NET.

Can't you download the vb6 version and use the upgrade wizard to
convert it for you??
nate axtell - 20 Oct 2004 13:02 GMT
Search for "TreeView" in this linked page:
http://www.vb-helper.com/index_files_and_directories.html
There are a few examples that may help you.
-nate

> How do I save/load the contents of a Treeview to a file?
> I have found several good examples written i VB6, but not a single one for
[quoted text clipped - 4 lines]
> ----
> Tim
BluDog - 20 Oct 2004 14:35 GMT
>How do I save/load the contents of a Treeview to a file?
>I have found several good examples written i VB6, but not a single one for
[quoted text clipped - 4 lines]
>----
>Tim

The following uses a couple of strucures to represent the Treeview in
it's simplest form and then serialization to persist the data to file:

<Code>

<Serializable()> Public Structure TreeViewData
   Public Nodes() As TreeNodeData

   Public Sub New(ByVal treeview As TreeView)
       If treeview.Nodes.Count = 0 Then Exit Sub

       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

   Public Sub PopulateTree(ByVal treeview As TreeView)

       If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit
Sub
       For i As Integer = 0 To Me.Nodes.Length - 1
           treeview.Nodes.Add(Me.Nodes(i).ToTreeNode)
       Next

   End Sub

End Structure

<Serializable()> Public Structure TreeNodeData

   Public Text As String
   Public ImageIndex As Integer
   Public SelectedImageIndex As Integer
   Public Nodes() As TreeNodeData

   Public Sub New(ByVal node As TreeNode)

       Me.Text = node.Text
       Me.ImageIndex = node.ImageIndex
       Me.SelectedImageIndex = node.SelectedImageIndex

       If node.Nodes.Count = 0 Then Exit Sub

       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

   Public Function ToTreeNode() As TreeNode
       ToTreeNode = New TreeNode(Me.Text, Me.ImageIndex,
Me.SelectedImageIndex)

       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(Me.Nodes(i).ToTreeNode)
       Next
   End Function

End Structure

</Code>

Then to use it:

<Code>

   Private SubLoadButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click

       Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
       Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.OpenOrCreate)
       Dim writer As New System.Xml.XmlTextWriter(file, Nothing)

       ser.Serialize(writer, New TreeViewData(TreeView1))

       writer.Close()
       file.Close()
       file = Nothing

   End Sub

   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button4.Click

       Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
       Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.Open)
       Dim reader As New System.Xml.XmlTextReader(file)

       Dim treeData As TreeViewData = CType(ser.Deserialize(reader),
TreeViewData)
       treeData.PopulateTree(TreeView1)

       reader.Close()
       file.Close()
       file = Nothing

   End Sub

</Code>

Hope this helps

Blu.
BluDog - 20 Oct 2004 14:51 GMT
>>How do I save/load the contents of a Treeview to a file?
>>I have found several good examples written i VB6, but not a single one for
[quoted text clipped - 4 lines]
>>----
>>Tim

>The following uses a couple of strucures to represent the Treeview in
>it's simplest form and then serialization to persist the data to file:

Sorry... couple of errors there on testing... try this.

><Code>

<Serializable()> Public Structure TreeViewData
   Public Nodes() As TreeNodeData

   Public Sub New(ByVal treeview As TreeView)
       If treeview.Nodes.Count = 0 Then Exit Sub

       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

   Public Sub PopulateTree(ByVal treeview As TreeView)

       If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit
Sub
       For i As Integer = 0 To Me.Nodes.Length - 1
           treeview.Nodes.Add(Me.Nodes(i).ToTreeNode)
       Next

   End Sub

End Structure

<Serializable()> Public Structure TreeNodeData

   Public Text As String
   Public ImageIndex As Integer
   Public SelectedImageIndex As Integer
   Public Nodes() As TreeNodeData

   Public Sub New(ByVal node As TreeNode)

       Me.Text = node.Text
       Me.ImageIndex = node.ImageIndex
       Me.SelectedImageIndex = node.SelectedImageIndex

       If node.Nodes.Count = 0 Then Exit Sub

       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

   Public Function ToTreeNode() As TreeNode
       ToTreeNode = New TreeNode(Me.Text, Me.ImageIndex,
Me.SelectedImageIndex)

       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(Me.Nodes(i).ToTreeNode)
       Next
   End Function

End Structure>

></Code>
>
>Then to use it:
>
><Code>

   Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles SaveButton.Click

       Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
       Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.Create)
       Dim writer As New System.Xml.XmlTextWriter(file, Nothing)

       ser.Serialize(writer, New TreeViewData(TreeView1))

       writer.Close()
       file.Close()
       file = Nothing

   End Sub

   Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles LoadButton.Click

       Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
       Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.Open)
       Dim reader As New System.Xml.XmlTextReader(file)

       Dim treeData As TreeViewData = CType(ser.Deserialize(reader),
TreeViewData)
       treeData.PopulateTree(TreeView1)

       reader.Close()
       file.Close()
       file = Nothing

   End Sub

></Code>
>
>Hope this helps
>
>Blu.
Mr.D - 20 Oct 2004 15:04 GMT
> The following uses a couple of strucures to represent the Treeview in
> it's simplest form and then serialization to persist the data to file:
>
> <Code></Code>

Outstanding code!
It ran directly out of the box :o)

Thanks for the help!

> Hope this helps
> Blu.

You are a lifesaver.

----
Tim
Tom John - 21 Oct 2004 14:38 GMT
Tim

I have tidied it up into a better class and posted the sourcecode in
Zip format:

http://www.codeproject.com/useritems/TreeViewDataAccess/TreeViewDataAccess_src.zip

There is a work in progress article here:

http://www.codeproject.com/useritems/TreeViewDataAccess.asp

Hope this is useful.

Blu
Mr.D - 21 Oct 2004 22:34 GMT
> I have tidied it up into a better class and posted the sourcecode in
> Zip format:

http://www.codeproject.com/useritems/TreeViewDataAccess/TreeViewDataAccess_src.zip

> There is a work in progress article here:
>
> http://www.codeproject.com/useritems/TreeViewDataAccess.asp
>
> Hope this is useful.

I get a 404 on both links, but maby codeproject.com just needs to refresh
some files.
I'll check by later.

The code does however works outstaningly well.

----
Tim

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.