help me ,我用TreeView控件如何加载全国城市信息
我想添加全国城市信息,但是太多了,一条一条添加的话,太麻烦了,有什么好的办法吗
2014-04-09 10:13
程序代码:Option Explicit
Dim path As String '系统路径
Const DataFile = "区划对照表.csv" '数据文件名,常量
Dim nodeselect As MSComctlLib.Node '当前选择的数据
Private Sub Command1_Click()
'提示信息
MsgBox "区划名:" & nodeselect.Text & vbCrLf & "区划代码:" & Mid(nodeselect.Key, 2)
End Sub
Private Sub Form_Load()
'初始化路径,确保格式统一
path = App.path
If Right(path, 1) <> "\" Then
path = path & "\"
End If
'数据文件全路径
Dim f As String
f = path & DataFile
Dim s As String
Dim fj() As String
Dim s1 As String, s2 As String
Dim nodx As Node
'读数据,生成树
Open f For Input Access Read As #1 '打开文件,只读
Do While Not EOF(1) '没结束则继续循环
Line Input #1, s '读一行
If InStr(1, s, ",") > 0 Then '包含逗号
fj = Split(s, ",") '分解数据
If IsNumeric(fj(0)) Then '第一段是数值类型
If Val(Mid(fj(0), 3)) = 0 Then '省名字,后四位全为 0
Set nodx = TreeView1.Nodes.Add(, , "K" & fj(0), fj(1)) '添加为顶级
s1 = "K" & fj(0) '保存省级Key
ElseIf Val(Right(fj(0), 2)) > 0 Then '县名字 '后2位不为0的为县级
Set nodx = TreeView1.Nodes.Add(s2, tvwChild, "K" & fj(0), fj(1)) '添加为市级的下一级
Else
Set nodx = TreeView1.Nodes.Add(s1, tvwChild, "K" & fj(0), fj(1)) '其他的为市级,后2位为0,第 3 4 位不为零
s2 = "K" & fj(0) '保存市级 Key
End If
End If
End If
Loop
Close #1
Set nodeselect = TreeView1.Nodes(1) '默认是选择了第一个
nodeselect.Expanded = True '第一个展开
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Set nodeselect = Node '设置选择这个
Node.Expanded = True '展开
End Sub

2014-04-09 12:35
2014-04-09 13:42
2014-04-09 13:42