Option Strict On Imports System.IO Imports System.Runtime.Serialization.Formatters.Soap Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents bttnListCities As System.Windows.Forms.Button Friend WithEvents ListBox1 As System.Windows.Forms.ListBox Friend WithEvents bttnListContinents As System.Windows.Forms.Button Friend WithEvents bttnListCountries As System.Windows.Forms.Button Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox Friend WithEvents txtContinent As System.Windows.Forms.TextBox Friend WithEvents bttnAddNode As System.Windows.Forms.Button Friend WithEvents txtCountry As System.Windows.Forms.TextBox Friend WithEvents txtCity As System.Windows.Forms.TextBox Friend WithEvents bttnExpandNode As System.Windows.Forms.Button Friend WithEvents bttnDelNode As System.Windows.Forms.Button Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents FileMenu As System.Windows.Forms.MenuItem Friend WithEvents FileLoad As System.Windows.Forms.MenuItem Friend WithEvents FileSave As System.Windows.Forms.MenuItem Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents GlobeTree As System.Windows.Forms.TreeView Private components As System.ComponentModel.IContainer 'Required by the Windows Form Designer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Private Sub InitializeComponent() Me.bttnListCities = New System.Windows.Forms.Button() Me.txtContinent = New System.Windows.Forms.TextBox() Me.FileSave = New System.Windows.Forms.MenuItem() Me.bttnDelNode = New System.Windows.Forms.Button() Me.bttnExpandNode = New System.Windows.Forms.Button() Me.bttnAddNode = New System.Windows.Forms.Button() Me.txtCity = New System.Windows.Forms.TextBox() Me.GroupBox1 = New System.Windows.Forms.GroupBox() Me.ListBox1 = New System.Windows.Forms.ListBox() Me.bttnListContinents = New System.Windows.Forms.Button() Me.bttnListCountries = New System.Windows.Forms.Button() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.FileMenu = New System.Windows.Forms.MenuItem() Me.FileLoad = New System.Windows.Forms.MenuItem() Me.GroupBox3 = New System.Windows.Forms.GroupBox() Me.txtCountry = New System.Windows.Forms.TextBox() Me.Button1 = New System.Windows.Forms.Button() Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.GlobeTree = New System.Windows.Forms.TreeView() Me.GroupBox1.SuspendLayout() Me.GroupBox3.SuspendLayout() Me.SuspendLayout() ' 'bttnListCities ' Me.bttnListCities.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnListCities.Location = New System.Drawing.Point(106, 382) Me.bttnListCities.Name = "bttnListCities" Me.bttnListCities.Size = New System.Drawing.Size(152, 32) Me.bttnListCities.TabIndex = 6 Me.bttnListCities.Text = "ListCities" ' 'txtContinent ' Me.txtContinent.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtContinent.Location = New System.Drawing.Point(8, 16) Me.txtContinent.Name = "txtContinent" Me.txtContinent.Size = New System.Drawing.Size(152, 22) Me.txtContinent.TabIndex = 2 Me.txtContinent.Text = "" ' 'FileSave ' Me.FileSave.Index = 1 Me.FileSave.Text = "Save Nodes" ' 'bttnDelNode ' Me.bttnDelNode.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnDelNode.Location = New System.Drawing.Point(8, 288) Me.bttnDelNode.Name = "bttnDelNode" Me.bttnDelNode.Size = New System.Drawing.Size(152, 32) Me.bttnDelNode.TabIndex = 6 Me.bttnDelNode.Text = "Delete Current Node" ' 'bttnExpandNode ' Me.bttnExpandNode.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnExpandNode.Location = New System.Drawing.Point(168, 288) Me.bttnExpandNode.Name = "bttnExpandNode" Me.bttnExpandNode.Size = New System.Drawing.Size(152, 32) Me.bttnExpandNode.TabIndex = 6 Me.bttnExpandNode.Text = "Expand Current Node" ' 'bttnAddNode ' Me.bttnAddNode.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnAddNode.Location = New System.Drawing.Point(152, 112) Me.bttnAddNode.Name = "bttnAddNode" Me.bttnAddNode.Size = New System.Drawing.Size(152, 32) Me.bttnAddNode.TabIndex = 5 Me.bttnAddNode.Text = "Add This Node" ' 'txtCity ' Me.txtCity.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtCity.Location = New System.Drawing.Point(152, 80) Me.txtCity.Name = "txtCity" Me.txtCity.Size = New System.Drawing.Size(152, 22) Me.txtCity.TabIndex = 4 Me.txtCity.Text = "" ' 'GroupBox1 ' Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnListCities, Me.ListBox1, Me.bttnListContinents, Me.bttnListCountries}) Me.GroupBox1.Location = New System.Drawing.Point(336, 8) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(362, 424) Me.GroupBox1.TabIndex = 7 Me.GroupBox1.TabStop = False ' 'ListBox1 ' Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.ListBox1.ItemHeight = 14 Me.ListBox1.Location = New System.Drawing.Point(16, 24) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(334, 256) Me.ListBox1.TabIndex = 1 ' 'bttnListContinents ' Me.bttnListContinents.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnListContinents.Location = New System.Drawing.Point(106, 286) Me.bttnListContinents.Name = "bttnListContinents" Me.bttnListContinents.Size = New System.Drawing.Size(152, 32) Me.bttnListContinents.TabIndex = 6 Me.bttnListContinents.Text = "ListContinents" ' 'bttnListCountries ' Me.bttnListCountries.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnListCountries.Location = New System.Drawing.Point(106, 334) Me.bttnListCountries.Name = "bttnListCountries" Me.bttnListCountries.Size = New System.Drawing.Size(152, 32) Me.bttnListCountries.TabIndex = 6 Me.bttnListCountries.Text = "ListCountries" ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FileMenu}) ' 'FileMenu ' Me.FileMenu.Index = 0 Me.FileMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FileLoad, Me.FileSave}) Me.FileMenu.Text = "File" ' 'FileLoad ' Me.FileLoad.Index = 0 Me.FileLoad.Text = "Load Nodes" ' 'GroupBox3 ' Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtContinent, Me.bttnAddNode, Me.txtCountry, Me.txtCity}) Me.GroupBox3.Location = New System.Drawing.Point(8, 328) Me.GroupBox3.Name = "GroupBox3" Me.GroupBox3.Size = New System.Drawing.Size(312, 152) Me.GroupBox3.TabIndex = 9 Me.GroupBox3.TabStop = False ' 'txtCountry ' Me.txtCountry.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtCountry.Location = New System.Drawing.Point(80, 48) Me.txtCountry.Name = "txtCountry" Me.txtCountry.Size = New System.Drawing.Size(152, 22) Me.txtCountry.TabIndex = 3 Me.txtCountry.Text = "" ' 'Button1 ' Me.Button1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Button1.Location = New System.Drawing.Point(444, 440) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(152, 40) Me.Button1.TabIndex = 10 Me.Button1.Text = "Process Selected Nodes" ' 'SaveFileDialog1 ' Me.SaveFileDialog1.FileName = "doc1" ' 'GlobeTree ' Me.GlobeTree.CheckBoxes = True Me.GlobeTree.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.GlobeTree.ImageIndex = -1 Me.GlobeTree.Location = New System.Drawing.Point(8, 16) Me.GlobeTree.Name = "GlobeTree" Me.GlobeTree.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Globe", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Africa"), New System.Windows.Forms.TreeNode("Asia"), New System.Windows.Forms.TreeNode("Europe"), New System.Windows.Forms.TreeNode("N. America"), New System.Windows.Forms.TreeNode("Oceania"), New System.Windows.Forms.TreeNode("S. America")})}) Me.GlobeTree.SelectedImageIndex = -1 Me.GlobeTree.Size = New System.Drawing.Size(312, 264) Me.GlobeTree.TabIndex = 0 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(644, 462) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.GlobeTree, Me.bttnExpandNode, Me.bttnDelNode, Me.GroupBox3, Me.GroupBox1}) Me.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "GLOBE" Me.GroupBox1.ResumeLayout(False) Me.GroupBox3.ResumeLayout(False) Me.ResumeLayout(False) End Sub #End Region ' The project assumes that the continent names have been added to the TreeView control ' at design time. In the Form's Load event it adds country names under each continent ' and city names under each county Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim GlobeNode As TreeNode Dim ContinentNode As TreeNode GlobeNode = GlobeTree.Nodes(0) Dim CountryNode As TreeNode For Each ContinentNode In GlobeNode.Nodes Select Case ContinentNode.Text Case "Europe" CountryNode = ContinentNode.Nodes.Add("Germany") CountryNode.Nodes.Add("Berlin") CountryNode.Nodes.Add("Munich") CountryNode.Nodes.Add("Bayreuth") CountryNode.Nodes.Add("Frankfurt") CountryNode = ContinentNode.Nodes.Add("France") CountryNode.Nodes.Add("Paris") CountryNode.Nodes.Add("Nice") CountryNode.Nodes.Add("Lyon") CountryNode = ContinentNode.Nodes.Add("Italy") CountryNode.Nodes.Add("Rome") CountryNode.Nodes.Add("Venice") CountryNode.Nodes.Add("Napoli") CountryNode.Nodes.Add("Florence") CountryNode.Nodes.Add("Milan") CountryNode = ContinentNode.Nodes.Add("Spain") CountryNode.Nodes.Add("Madrid") CountryNode.Nodes.Add("Barcelona") CountryNode.Nodes.Add("Malaga") Case "Asia" CountryNode = ContinentNode.Nodes.Add("China") CountryNode.Nodes.Add("Bejing") CountryNode.Nodes.Add("Nanjing") CountryNode.Nodes.Add("Hong Kong") CountryNode = ContinentNode.Nodes.Add("Japan") CountryNode.Nodes.Add("Tokyo") CountryNode.Nodes.Add("Osaka") CountryNode.Nodes.Add("Kobe") CountryNode.Nodes.Add("Kyotto") CountryNode.Nodes.Add("Sapporo") Case "Africa" CountryNode = ContinentNode.Nodes.Add("Egypt") CountryNode.Nodes.Add("Cairo") CountryNode.Nodes.Add("Alexandria") CountryNode = ContinentNode.Nodes.Add("Libya") CountryNode.Nodes.Add("Tripoli") CountryNode = ContinentNode.Nodes.Add("South Africa") CountryNode.Nodes.Add("Johannesburg") CountryNode.Nodes.Add("Pretoria") CountryNode = ContinentNode.Nodes.Add("Zimbabwe") CountryNode.Nodes.Add("Harare") Case "Oceania" CountryNode = ContinentNode.Nodes.Add("Australia") CountryNode.Nodes.Add("Sidney") CountryNode.Nodes.Add("Melbourne") CountryNode.Nodes.Add("Adelaida") CountryNode.Nodes.Add("Brisbane") CountryNode.Nodes.Add("Canberra") CountryNode = ContinentNode.Nodes.Add("New Zealand") CountryNode.Nodes.Add("Wanaka") Case "N. America" CountryNode = ContinentNode.Nodes.Add("USA") CountryNode.Nodes.Add("New York") CountryNode.Nodes.Add("Las Vegas") CountryNode.Nodes.Add("Chicago") CountryNode.Nodes.Add("Washington") CountryNode.Nodes.Add("Durham") CountryNode.Nodes.Add("San Francisco") CountryNode = ContinentNode.Nodes.Add("Canada") CountryNode.Nodes.Add("Toronto") CountryNode.Nodes.Add("Vancouver") CountryNode.Nodes.Add("Montreal") CountryNode.Nodes.Add("Halifax") CountryNode.Nodes.Add("Calgary") CountryNode = ContinentNode.Nodes.Add("Mexico") CountryNode.Nodes.Add("Mexico City") Case "S. America" CountryNode = ContinentNode.Nodes.Add("Argentina") CountryNode.Nodes.Add("Buenos Aires") CountryNode = ContinentNode.Nodes.Add("Peru") CountryNode.Nodes.Add("Lima") CountryNode = ContinentNode.Nodes.Add("Colombia") CountryNode.Nodes.Add("Bogota") End Select Next End Sub Private Sub bttnAddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnAddNode.Click Dim nd As TreeNode Dim Continents As TreeNode ' If the first TextBox contains a continent name, then search all continents to locate ' the specified continent. If txtContinent.Text.Trim <> "" Then Continents = GlobeTree.Nodes(0) Dim ContinentFound, CountryFound, CityFound As Boolean Dim ContinentNode, CountryNode, CityNode As TreeNode For Each nd In Continents.Nodes Console.WriteLine(nd.Text) If nd.Text.ToUpper = txtContinent.Text.ToUpper Then ContinentFound = True Exit For End If Next ' If the specified continent wasn't found, add it to the control If Not ContinentFound Then nd = Continents.Nodes.Add(txtContinent.Text) End If ContinentNode = nd ' If the second TextBox contains a country name, then search all countries under ' the current continent. If txtCountry.Text.Trim <> "" Then Dim Countries As TreeNode Countries = ContinentNode If Not Countries Is Nothing Then For Each nd In Countries.Nodes Console.WriteLine(nd.Text) If nd.Text.ToUpper = txtCountry.Text.ToUpper Then CountryFound = True Exit For End If Next End If ' If the specified country wasn't found, add it under the current continent If Not CountryFound Then nd = ContinentNode.Nodes.Add(txtCountry.Text) End If CountryNode = nd ' If the third TextBox contains a city name, then search all cities under ' the current country If txtCity.Text.Trim <> "" Then Dim Cities As TreeNode Cities = CountryNode If Not Cities Is Nothing Then For Each nd In Cities.Nodes Console.WriteLine(nd.Text) If nd.Text.ToUpper = txtCity.Text.ToUpper Then CityFound = True Exit For End If Next End If ' if the specified city wasn't found, add it under the current country If Not CityFound Then nd = CountryNode.Nodes.Add(txtCity.Text) End If CityNode = nd End If End If End If End Sub Private Sub bttnListContinents_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnListContinents.Click Dim Nd As TreeNode, continentNode As TreeNode Dim continent As Integer, continents As Integer ListBox1.Items.Clear() Nd = GlobeTree.Nodes(0) continents = Nd.Nodes.Count continentNode = Nd.Nodes(0) For continent = 1 To continents ListBox1.Items.Add(continentNode.Text) continentNode = continentNode.NextNode Next End Sub Private Sub bttnListCountries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnListCountries.Click Dim Nd As TreeNode, CountryNode As TreeNode, ContinentNode As TreeNode Dim continent As Integer, continents As Integer Dim country As Integer, countries As Integer ListBox1.Items.Clear() Nd = GlobeTree.Nodes.Item(0) continents = Nd.Nodes.Count ContinentNode = Nd.Nodes(0) ' To locate all the countries, scan each continent ... For continent = 1 To continents countries = ContinentNode.Nodes.Count CountryNode = ContinentNode.Nodes(0) ' ... and all child nodes under the current continent For country = 1 To countries ListBox1.Items.Add(CountryNode.Text) CountryNode = CountryNode.NextNode Next ContinentNode = ContinentNode.NextNode Next End Sub Private Sub bttnListCities_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnListCities.Click Dim Nd As TreeNode, countryNode As TreeNode, continentNode As TreeNode, cityNode As TreeNode Dim continent As Integer, continents As Integer Dim country As Integer, countries As Integer Dim city As Integer, cities As Integer ListBox1.Items.Clear() Nd = GlobeTree.Nodes(0) continents = Nd.Nodes.Count continentNode = Nd.Nodes(0) ' To locate all the cities, scan each continent ... For continent = 1 To continents countries = continentNode.Nodes.Count countryNode = continentNode.Nodes(0) ' ... all the child nodes under the current continent ... For country = 1 To countries cities = countryNode.Nodes.Count cityNode = countryNode.Nodes(0) ' ... and all the child nodes under the current city For city = 1 To cities ListBox1.Items.Add(cityNode.Text) cityNode = cityNode.NextNode Next countryNode = countryNode.NextNode Next continentNode = continentNode.NextNode Next End Sub Private Sub bttnDelNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDelNode.Click GlobeTree.SelectedNode.Remove() ' Removes the selected node txtContinent.Text = "" txtCountry.Text = "" txtCity.Text = "" End Sub Private Sub bttnExpandNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnExpandNode.Click GlobeTree.SelectedNode.Expand() End Sub Private Sub GlobeTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles GlobeTree.AfterSelect If GlobeTree.SelectedNode Is Nothing Then Exit Sub ' every time a node is selected with the mouse, we display its parts (continent/country/city) ' on the three TextBox controls on the form Dim components() As String txtContinent.Text = "" txtCountry.Text = "" txtCity.Text = "" components = Split(GlobeTree.SelectedNode.FullPath.ToString, GlobeTree.PathSeparator) Console.WriteLine(GlobeTree.SelectedNode.FullPath.ToString) If components.Length > 1 Then txtContinent.Text = components(1) If components.Length > 2 Then txtCountry.Text = components(2) If components.Length > 3 Then txtCity.Text = components(3) End Sub Private Sub bttnAddNode_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnAddNode.Click Dim nd As TreeNode Dim Continents As TreeNode ' Add continent, if it's not on the control already ' If no continent name was specified, none of the following statements will be executed If txtContinent.Text.Trim <> "" Then Continents = GlobeTree.Nodes(0) Dim ContinentFound, CountryFound, CityFound As Boolean Dim ContinentNode, CountryNode, CityNode As TreeNode For Each nd In Continents.Nodes If nd.Text.ToUpper = txtContinent.Text.ToUpper Then ContinentFound = True Exit For End If Next If Not ContinentFound Then nd = Continents.Nodes.Add(txtContinent.Text) End If ContinentNode = nd ' Add country, if it's no on the control already ' If no country was specified, none of the following statements will be executed If txtCountry.Text.Trim <> "" Then Dim Countries As TreeNode Countries = ContinentNode If Not Countries Is Nothing Then For Each nd In Countries.Nodes If nd.Text.ToUpper = txtCountry.Text.ToUpper Then CountryFound = True Exit For End If Next End If If Not CountryFound Then nd = ContinentNode.Nodes.Add(txtCountry.Text) End If CountryNode = nd ' Add city, if it's not on the control already If txtCity.Text.Trim <> "" Then Dim Cities As TreeNode Cities = CountryNode If Not Cities Is Nothing Then For Each nd In Cities.Nodes If nd.Text.ToUpper = txtCity.Text.ToUpper Then CityFound = True Exit For End If Next End If If Not CityFound Then nd = CountryNode.Nodes.Add(txtCity.Text) End If CityNode = nd End If End If End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim continent, country, city As TreeNode Dim Continents, Countries, Cities As TreeNodeCollection ListBox1.Items.Clear() Continents = GlobeTree.Nodes(0).Nodes For Each continent In Continents If continent.Checked Then ListBox1.Items.Add(continent.FullPath) Countries = continent.Nodes For Each country In Countries If country.Checked Or country.Parent.Checked Then _ ListBox1.Items.Add(" " & country.FullPath) Cities = country.Nodes For Each city In Cities If city.Checked Or city.Parent.Checked Or _ city.Parent.Parent.Checked Then _ ListBox1.Items.Add(" " & _ city.FullPath) Next Next Next End Sub Private Sub FileLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileLoad.Click OpenFileDialog1.DefaultExt = "XML" If OpenFileDialog1.ShowDialog = DialogResult.OK Then ' LoadNodes(GlobeTree, OpenFileDialog1.FileName) ' The following statements persists the globe structure using ' the NodeSerializer class's methods. Unocomment the following two statements ' to test the functionality of the NodeSerializer class Dim NS As New NodeSerializer() NS.LoadNodes(GlobeTree, OpenFileDialog1.FileName) End If End Sub Private Sub FileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSave.Click SaveFileDialog1.DefaultExt = "XML" If SaveFileDialog1.ShowDialog = DialogResult.OK Then 'CreateList(GlobeTree.Nodes(0), SaveFileDialog1.FileName) ' The following statements load the globe structure to the TreeView control ' using the NodeSerializer class's methods. Unocomment the following two statements ' to test the functionality of the NodeSerializer class Dim NS As New NodeSerializer() NS.CreateList(GlobeTree.Nodes(0), SaveFileDialog1.FileName) End If End Sub Structure sNode Dim node As String Dim level As Integer End Structure Dim GlobeNodes As New ArrayList() ' The LoadNodes() subroutine reads a text file with node values (strings) ' into an ArrayList and then calls the showNodes() subroutine to add the nodes ' to the TreeView control ' The LoadNodes() subroutine reads the nodes saved to the file by calling a ' SoapFormatter object's Deserialize method Sub LoadNodes(ByVal TV As TreeView, ByVal fName As String) TV.Nodes.Clear() Dim formatter As SoapFormatter Dim openFile As FileStream openFile = File.Open(fName, FileMode.Open) formatter = New SoapFormatter() GlobeNodes = CType(formatter.Deserialize(openFile), ArrayList) openFile.Close() showNodes(TV) End Sub ' The showNodes() subroutine reconstructs the original tree from the ' contents of the GlobeNodes ArrayList. Each item contains the node's text a\ ' and the node's level Sub showNodes(ByVal TV As TreeView) Dim o As Object Dim currNode As TreeNode Dim level As Integer = 0 Dim fromLowerLevel As Integer Dim i As Integer For i = 0 To GlobeNodes.Count - 1 o = GlobeNodes(i) ' IF THE STRICT OPTION IS OFF, YOU CAN USE THE O OBJECT (AND NOT THE ONODE OBJECT) ' IN THE REST OF THE CODE, AS SHOWN IN THE TEXT Dim oNode As sNode = CType(o, sNode) If CType(oNode, sNode).level = level Then If currNode Is Nothing Then currNode = TV.Nodes.Add(oNode.node.ToString) Else currNode = currNode.Parent.Nodes.Add(oNode.node.ToString) End If Else If oNode.level > level Then currNode = currNode.Nodes.Add(oNode.node.ToString) level = oNode.level Else While oNode.level <= level currNode = currNode.Parent level = level - 1 End While currNode = currNode.Nodes.Add(oNode.node.ToString) End If End If TV.ExpandAll() Application.DoEvents() Next End Sub ' The CreateList() subroutine reads the nodes of the TreeView control passed as argument ' and stores them as sNode objects to an ArrayList ' Each sNode object contains the node's text and the node's level ' After populating the GlobeNodes ArrayList, it calls the SaveNodes() subroutine ' to serialize the ArrayList to a disk file. ' The file's name is passed to the SaveNodes() subroutine as argument Sub CreateList(ByVal node As TreeNode, ByVal fName As String) Static level As Integer Dim thisNode As TreeNode Dim myNode As sNode Application.DoEvents() myNode.level = level myNode.node = node.Text GlobeNodes.Add(myNode) If node.Nodes.Count > 0 Then level = level + 1 For Each thisNode In node.Nodes CreateList(thisNode, fName) Next level = level - 1 End If SaveNodes(fName) End Sub ' The SaveNodes() subroutine saves the GlobeNodes ArrayList to a disk file ' by calling a SoapFormatter object's Serialize method Sub SaveNodes(ByVal fName As String) Dim formatter As SoapFormatter Dim saveFile As FileStream saveFile = File.Create(fName) formatter = New SoapFormatter() formatter.Serialize(saveFile, GlobeNodes) saveFile.Close() End Sub End Class