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 Msg
Box(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就行 __________________________________________________________________________