TreeView 类
https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.treeview?view=netcore-3.1
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub btnReadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadData.Click
TreeView1.Nodes.Clear()
TreeView1.CheckBoxes = True
Dim first As New TreeNode("一级指标")
TreeView1.Nodes.Add(first)
Dim node1 As TreeNode = New TreeNode("学术队伍")
Dim node2 As TreeNode = New TreeNode("科学研究")
Dim node3 As TreeNode = New TreeNode("人才培养")
Dim node4 As TreeNode = New TreeNode("学术声誉")
Dim node5 As TreeNode = New TreeNode("客观数据")
first.Nodes.Add(node1)
first.Nodes.Add(node2)
first.Nodes.Add(node3)
first.Nodes.Add(node4)
first.Nodes.Add(node5)
first.ExpandAll()
Dim Second As TreeNode = New TreeNode("二级指标")
TreeView1.Nodes.Add(Second)
Dim secNode1 As TreeNode = New TreeNode("科研基础")
secNode1.Checked = True
Dim secNode2 As TreeNode = New TreeNode("获奖专利")
Dim secNode3 As TreeNode = New TreeNode("科研经费")
Dim secNode4 As TreeNode = New TreeNode("学生情况")
Dim secNode5 As TreeNode = New TreeNode("论文专著")
Dim secNode6 As TreeNode = New TreeNode("论文专著")
Second.Nodes.Add(secNode1)
Second.Nodes.Add(secNode2)
Second.Nodes.Add(secNode3)
Second.Nodes.Add(secNode4)
Second.Nodes.Add(secNode5)
Second.Nodes.Add(secNode6)
Second.ExpandAll()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
TreeView1.Nodes.Remove(TreeView1.SelectedNode)
End Sub
Private Sub btnSaveModel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveModel.Click
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
If SaveFileDialog1.FileName.Equals("") Then
Return
End If
Dim rw As StreamWriter = File.CreateText(SaveFileDialog1.FileName)
If TreeView1.Nodes(0).Checked Then
rw.WriteLine(TreeView1.Nodes(0).Text)
Dim i As Integer = 0
For i = 0 To TreeView1.Nodes(0).Nodes.Count - 1
If TreeView1.Nodes(0).Nodes(i).Checked Then
rw.WriteLine(TreeView1.Nodes(0).Nodes(i).Text)
End If
Next
End If
If TreeView1.Nodes(1).Checked Then
rw.WriteLine(TreeView1.Nodes(1).Text)
Dim i As Integer = 0
For i = 0 To TreeView1.Nodes(1).Nodes.Count - 1
If TreeView1.Nodes(1).Nodes(i).Checked Then
rw.WriteLine(TreeView1.Nodes(1).Nodes(i).Text)
End If
Next
End If
rw.Flush()
rw.Close()
End If
End Sub
'读取数据后才能高度成功
Private Sub btnReadModel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadModel.Click
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
If OpenFileDialog1.FileName.Equals("") Then
Return
End If
Dim sr As StreamReader = File.OpenText(OpenFileDialog1.FileName)
Dim output As String = ""
Do
output +=sr.Readline +vbCrlf
Loop While sr.Peek <> -1
Dim f As Integer = 0
For f = 0 To TreeView1.Nodes.Count - 1
If output.IndexOf(TreeView1.Nodes(f).Text) <> -1 Then
TreeView1.Nodes(f).Checked = True
Else
TreeView1.Nodes(f).Checked = False
End If
Next
For f = 0 To TreeView1.Nodes(0).Nodes.Count - 1
If output.IndexOf(TreeView1.Nodes(0).Nodes(f).Text) <> -1 Then
TreeView1.Nodes(0).Nodes(f).Checked = True
Else
TreeView1.Nodes(0).Nodes(f).Checked = False
End If
Next
For f = 0 To TreeView1.Nodes(1).Nodes.Count - 1
If output.IndexOf(TreeView1.Nodes(1).Nodes(f).Text) <> -1 Then
TreeView1.Nodes(1).Nodes(f).Checked = True
Else
TreeView1.Nodes(1).Nodes(f).Checked = False
End If
Next
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
TextBox2.Text = e.Node.Text
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
TextBox1.Text = e.Node.Text
End Sub
End Class