📄 form1.vb
字号:
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
<Serializable()> 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -