|
发表于 2020-8-23 18:30:01
|
显示全部楼层
这个可以吗?
TreeView 使用方法
Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
If e.Node.Checked = True Then
'处理父节点
If Not e.Node.Parent Is Nothing Then
e.Node.Parent.Checked = e.Node.Checked
End If
Else
'处理子节点
For Each Node As TreeNode In e.Node.Nodes
If Not Node Is Nothing Then
Node.Checked = e.Node.Checked
End If
Next
End If
TreeView1.ExpandAll()
End Sub
读取treeview的值
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To TreeView1.Nodes.Count - 1
RichTextBox1.Text = RichTextBox1.Text + TreeView1.Nodes(i).Text + Chr(13)
RichTextBox1.Text = RichTextBox1.Text + GetChildText(TreeView1.Nodes(i)) + Chr(13)
RichTextBox1.Text = RichTextBox1.Text + Chr(13)
Next i
End Sub
Private Function GetChildText(ByVal node As TreeNode) As String
Dim sb As New System.Text.StringBuilder()
Dim child As TreeNode
For Each child In node.Nodes
sb.Append(child.Text) sb.Append(Chr(9)).Append(child.Level).Append(Chr(9)).Append(child.FullPath.ToString).Append(Chr(9)).Append(Chr(13))
sb.Append(GetChildText(child))
Next child
Return sb.ToString()
End Function
添加数据到treeview
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SqlAdp1 As SqlDataAdapter
Dim SqlCommString As String
SqlCommString = "select * from sSystemMenu "
SqlAdp1 = New SqlDataAdapter(SqlCommString, ConnectionString)
Dim Dataset1 As New DataSet
SqlAdp1.Fill(Dataset1, "sSystemMenu")
TreeView1.Nodes.Clear()
CreateTreeNode(TreeView1.Nodes, "0\", Dataset1.Tables(0))
TreeView1.ExpandAll()
End Sub
Private Sub CreateTreeNode(ByVal nodes As TreeNodeCollection, ByVal parentid As String, ByVal dt As DataTable)
Dim tmp As String = "[MenuParentCode]='" & parentid & "'"
Dim rows() As DataRow = dt.Select(tmp)
If rows.Length > 0 Then
For i As Integer = 0 To rows.Length - 1
Dim node As New System.Windows.Forms.TreeNode
node.Tag = rows(i)
node.Text = Trim(rows(i).Item("Name")) + " [" + Trim(rows(i).Item("MenuCode")) + "]"
node.SelectedImageIndex = rows(i).Item("SelectPic")
node.ImageIndex = rows(i).Item("Pic")
node.Tag = Trim(rows(i).Item("MenuCode"))
nodes.Add(node)
CreateTreeNode(node.Nodes, rows(i).Item("MenuPathCode"), dt) '递归调用
Next
End If
End Sub
|
|