[VB.NET]VB中treeView问题,添加节点问题

前端之家收集整理的这篇文章主要介绍了[VB.NET]VB中treeView问题,添加节点问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
VB中treeView问题,添加节点问题 Public Function NodeLevel(ByVal n As TreeNode) As Byte 找出树中当前节点的级数 Dim i As Byte = 1 Dim m As String Do Until n.Parent Is Nothing n = n.Parent i += 1 Loop Return i End Function 添加结点 Public Sub fill_treeleaf(ByVal sql As String,ByVal _Name As String) Dim DS As New DataSet Dim i As Integer DS = GetDataFromDB(sql) Try If DS Is Nothing Then Else For i = 0 To DS.Tables(0).Rows.Count - 1 Dim tree_leaf As New TreeNode tree_leaf.Text = DS.Tables(0).Rows(i)(_Name) tree_leaf.Tag = DS.Tables(0).Rows(i)(_Name) Me.dbtreeView.SelectedNode.Nodes.Add(tree_leaf) Next End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub 查找要添加的结点 Private Sub dbtreeView_AfterSelect(ByVal sender As Object,ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles dbtreeView.AfterSelect Dim sqlStr As String Select Case NodeLevel(e.Node).ToString Case 1 If e.Node.GetNodeCount(False) = 0 Then If e.Node.Tag Is Nothing Then Else sqlStr = Select Distinct(floorNum) From I_Room Where Floorid= & e.Node.Tag fill_treeleaf(sqlStr,floorNum ) End If End If Case 2 If e.Node.GetNodeCount(False) = 0 Then If e.Node.Tag Is Nothing Then Else sqlStr = Select Distinct(floorRoom) From I_Room Where FloorNum= & e.Node.Tag fill_treeleaf(sqlStr,FloorRoom ) End If End If End Select End Sub 登录时的初始结点 Private Sub frmMain_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer Dim ds As New DataSet Dim sqlstr As String sqlstr = select floorid,floorName from I_floorinfo ds = GetDataFromDB(sqlstr) If ds Is Nothing Then Else For i = 0 To ds.Tables(0).Rows.Count - 1 Dim tree_root As New TreeNode tree_root.Text = ds.Tables(0).Rows(i)( floorname ) tree_root.Tag = ds.Tables(0).Rows(i)( floorid ) Me.dbtreeView.Nodes.Item(0).Nodes.Add(tree_root) (这里要是用Me.dbtreeView.Nodes.Add(tree_root)再点初始的结点时就能,出现子结点,要是用上面的就出不了子结点) Next End If End Sub 我先在treeView中直接加了一根ROOT结点,然后在登录时把ROOT结点下的几个子节点加上来,可是我再点子结点后就没有子结点现示了,要是把初始的子结点,加到ROOT同级时,再点初妈的子结点时,就能显示初始子结点下面的子结点 __________________________________________________________________________ 比较乱 __________________________________________________________________________ TreeView 不会有问题的,应该是你添加节点时候的问题,你在添加每个节点的时候输出它的级数和序号,看看是不是逻辑上面出问题了 __________________________________________________________________________ 谢谢neil_CN 是我的级数有问题,把case里的1和2改成2和3就行 __________________________________________________________________________ 原文链接:https://www.f2er.com/vb/263403.html

猜你在找的VB相关文章