01  江苏0101 南京市010101 鼓楼区010102 塘下区010103 其他区0102 苏州010201 相城区010202 苏州园区如何用VB 写代码,将这些数据做成一个树显示出来呢?

解决方案 »

  1.   

    窗体上放一个TreeView控件
    把你的数据保存到一个文本文件"城市.txt",注意排好顺序,例如:
    01  江苏
    0101 南京市
    010101 鼓楼区
    010102 塘下区
    010103 其他区
    0102 苏州
    010201 相城区
    010202 苏州园区
    02 河北
    0201 石家庄
    020101 1区
    020102 2区
    0202 保定
    020201 新区
    020202 西区
    03 天津
    0301 河西区
    0302 和平区Private Sub Command1_Click()
        Dim nd As Node, Root As Node, ndParent As Node
        Dim x() As String, s As String, sID As String, sPreID As String
        Dim sParentKey As String, sParentKeyOld As String
        Open App.Path & "\城市.txt" For Input As #1
        With TreeView1
            Set nd = .Nodes.Add()
            nd.Text = "全部城市"
            nd.Key = "root"
            sParentKey = "root"
            sParentKeyOld = "root"
            Set Root = nd
            Set ndParent = nd
            Do While Not EOF(1)
                Line Input #1, s
                x = Split(s, " ")
                sID = x(0)
                If Len(sID) > Len(sPreID) Then
                    Set ndParent = nd
                ElseIf Len(sID) < Len(sPreID) Then
                    If Left(sID, 2) = Left(sPreID, 2) Then
                        Set ndParent = ndParent.Parent
                    Else
                        Set ndParent = Root
                    End If
                End If
                Set nd = .Nodes.Add(ndParent.Key, tvwChild, "key" & sID, s)
                sPreID = sID
            Loop
            Root.Expanded = True
        End With
    End Sub